Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting HEX out of a QString and putting 2 Bytes together
QtWS25 Last Chance

Getting HEX out of a QString and putting 2 Bytes together

Scheduled Pinned Locked Moved Solved General and Desktop
sensor readingshex format
7 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SpaceToon
    wrote on last edited by
    #1

    Hi,

    I have a sensor that measures the orientation and acceleration in the x, y and z directions and sends them to my PC via BLE. The data that are sent are specified in HEX and packed in a string.
    Every two adjacent numbers result in a coordinate (consisting of the least significant byte and the most significant bit)

    //Example for acceleration
    //xLSB, xMSB, yLSB, yMSB, zLSB, zMSB
    "128, 62, 255, 107, 120, 20 "
    

    I save this printout in QT in a QString (called sensorValue). I would now like to save all values ​ in a list (preferably in HEX). I did not find how to do it in HEX, so I have this so far:

       QList<QString> myList = sensorValue.split(",");
    

    Then I want to merge every two neighboring bytes. In C this is done like this:

    msbAndLsb= (int16_t)(MSB << 8 | LSB);
    

    So, in my case something like:

    msbAndLsbX= (int16_t)(myList[1]<< 8 | myList[0]);
    msbAndLsbY= (int16_t)(myList[3]<< 8 | myList[2]);
    

    So how can I do this Shift thing with Qt? And how Can I convert these numbers in HEX so that I can work with it?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      Hi,

      You're talking about HEX but the string contains decimal values ?
      You want to save this values in HEX so you want to save it as a string , right ? Hum, i'm not sure :)

      Here some code to get what you want as int value:

      QByteArray bytes=QByteArray("128, 62, 255, 107, 120, 20 ");
         QList<QByteArray> array=bytes.split(',');
      
         for(int i=0; i<array.count()/2; i++)
             {
             int v=array[i].toInt()+(array[i+1].toInt()<<8);
             qDebug()<<v;
             }
      

      results:
      16000
      65342
      27647

      Carefully check the number of data in the array to prevent index out of bound exception.

      [EDIT]
      You can use QString in place of QByteArray

      WARNING
      replace
      for(int i=0; i<array.count()/2; i++)
      with
      for(int i=0; i<array.count(); i+=2)

      Thanks @JonB for stated my silly mistake :)

      S JonBJ 2 Replies Last reply
      2
      • M mpergand

        Hi,

        You're talking about HEX but the string contains decimal values ?
        You want to save this values in HEX so you want to save it as a string , right ? Hum, i'm not sure :)

        Here some code to get what you want as int value:

        QByteArray bytes=QByteArray("128, 62, 255, 107, 120, 20 ");
           QList<QByteArray> array=bytes.split(',');
        
           for(int i=0; i<array.count()/2; i++)
               {
               int v=array[i].toInt()+(array[i+1].toInt()<<8);
               qDebug()<<v;
               }
        

        results:
        16000
        65342
        27647

        Carefully check the number of data in the array to prevent index out of bound exception.

        [EDIT]
        You can use QString in place of QByteArray

        WARNING
        replace
        for(int i=0; i<array.count()/2; i++)
        with
        for(int i=0; i<array.count(); i+=2)

        Thanks @JonB for stated my silly mistake :)

        S Offline
        S Offline
        SpaceToon
        wrote on last edited by SpaceToon
        #3

        @mpergand

        Thank you very much, it's my fault. Yeah, the numbers are received in decimal. So my aim is to put two adjacent decimals "together" with the shift-oeprator and therefore I thought the numbers have to be converted in HEX.
        My only problem is, I did not get negative numbers (for example
        If I turn the sensor upside down, I should get a negative number for the negative acceleration, but I don't.) Maybe it has something to do with the conversion?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          try:

          int16_t v=array[i].toInt()+(array[i+1].toInt()<<8);
          

          16000
          -194
          27647

          1 Reply Last reply
          1
          • M mpergand

            Hi,

            You're talking about HEX but the string contains decimal values ?
            You want to save this values in HEX so you want to save it as a string , right ? Hum, i'm not sure :)

            Here some code to get what you want as int value:

            QByteArray bytes=QByteArray("128, 62, 255, 107, 120, 20 ");
               QList<QByteArray> array=bytes.split(',');
            
               for(int i=0; i<array.count()/2; i++)
                   {
                   int v=array[i].toInt()+(array[i+1].toInt()<<8);
                   qDebug()<<v;
                   }
            

            results:
            16000
            65342
            27647

            Carefully check the number of data in the array to prevent index out of bound exception.

            [EDIT]
            You can use QString in place of QByteArray

            WARNING
            replace
            for(int i=0; i<array.count()/2; i++)
            with
            for(int i=0; i<array.count(); i+=2)

            Thanks @JonB for stated my silly mistake :)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @mpergand said in Getting HEX out of a QString and putting 2 Bytes together:

            for(int i=0; i<array.count()/2; i++)

            Before the OP copies this, don't you mean

            for(int i=0; i<array.count(); i += 2)
            

            or have I been staring at this too long?!

            M 1 Reply Last reply
            1
            • JonBJ JonB

              @mpergand said in Getting HEX out of a QString and putting 2 Bytes together:

              for(int i=0; i<array.count()/2; i++)

              Before the OP copies this, don't you mean

              for(int i=0; i<array.count(); i += 2)
              

              or have I been staring at this too long?!

              M Offline
              M Offline
              mpergand
              wrote on last edited by
              #6

              @JonB said in Getting HEX out of a QString and putting 2 Bytes together:

              @mpergand said in Getting HEX out of a QString and putting 2 Bytes together:

              for(int i=0; i<array.count()/2; i++)

              Before the OP copies this, don't you mean

              for(int i=0; i<array.count(); i += 2)
              

              or have I been staring at this too long?!

              Damned you're right :)

              1 Reply Last reply
              1
              • S Offline
                S Offline
                SpaceToon
                wrote on last edited by
                #7

                Thank you both very much, it worked!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved