Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Getting HEX out of a QString and putting 2 Bytes together



  • 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?



  • 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 :)



  • 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 :)



  • @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?



  • try:

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

    16000
    -194
    27647



  • @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?!



  • @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 :)



  • Thank you both very much, it worked!