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

How to Split Hex String?



  • Greetings,

    I am receiving a string in Hex format in the following pattern:
    Incoming Data.JPG

    How can I split this string into relevant pieces of data?

    Looking forward to your kind responses.



  • QDataStream is the way to go:

    char hex[]="d5 0a 1b 73 0f fe 0f ff 00 af 0a 15 aa";
    QByteArray data=QByteArray::fromHex(hex);
    QDataStream stream(data);
    stream.setByteOrder(QDataStream::BigEndian);
    uint8_t bn, nb, esc, s, cs;
    uint16_t d1,d2,d3,d4;
    
    stream>>bn>>nb>>esc>>s;
    stream>>d1>>d2>>d3>>d4;
    stream>>cs;
    
    qDebug()<<bn<<nb<<esc<<s<<cs;
    qDebug()<<d1<<d2<<d3<<d4;
    

    logs:
    213 10 27 115 170
    4094 4095 175 2581


  • Moderators

    @ahsan737 how do you receive the data, why is it in a String in the first place and not a QByteArray?



  • @ahsan737
    So every 2 characters is a hex number for a byte. Read and convert those, or use QByteArray QByteArray::fromHex(const QByteArray &hexEncoded) to do the lot in one go.



  • I am receiving this data over the Bluetooth connection from another device. Before I had tried data transfer using "Bluetooth Terminal" apps available for android. But now I want to implement in Qt app for Android.



  • @JonB thank you, that's what I am confused about that how can I convert and split the data correctly.



  • @ahsan737
    I don't understand your question. I have stated what you should do.



  • Maybe show us some code of what you tried?



  • QDataStream is the way to go:

    char hex[]="d5 0a 1b 73 0f fe 0f ff 00 af 0a 15 aa";
    QByteArray data=QByteArray::fromHex(hex);
    QDataStream stream(data);
    stream.setByteOrder(QDataStream::BigEndian);
    uint8_t bn, nb, esc, s, cs;
    uint16_t d1,d2,d3,d4;
    
    stream>>bn>>nb>>esc>>s;
    stream>>d1>>d2>>d3>>d4;
    stream>>cs;
    
    qDebug()<<bn<<nb<<esc<<s<<cs;
    qDebug()<<d1<<d2<<d3<<d4;
    

    logs:
    213 10 27 115 170
    4094 4095 175 2581



  • @mpergand thank you so much, you've made my day.
    Now I am facing this issue (explained in the attached picture).
    outcomes.jpg

    I am not able to make changes to the incoming data format. So how can I parse the incoming data correctly?



  • @JonB sorry for the vague question but #mpergand has already responded to my query in great detail. Thank you for your kind response.



  • @ahsan737 I don't believe you are understanding what you are asking/doing!

    First, please learn what is a "Base Numbered System":

    • base 2 (aka Binary)
    • base 8 (aka Octal)
    • base 10 (aka Decimal)
    • base 16 (aka Hexadecimal)

    There are many, many explanation available on internet, for example ==> https://www.mathsisfun.com/binary-decimal-hexadecimal.html

    Then you will learn that:

    • 80 in hexadecimal equals 128 in decimal
    • 0A in hexadecimal equals 10 in decimal
    • etc.


  • @KroMignon You completely misunderstood the question. Of course, I totally understand this base numbered system. I am not talking about Hex to Dec conversion. My question is about string format as it is supposed to start with "80" but it is ending up at that, so I want to ask how can I sort it out? (given that I do not have access to change incoming data format)



  • @ahsan737 I don't understand what you are trying to do.

    QByteArray is a container for char (qint8).
    If you what to convert the content of a QByteArray variable into hex string, simply read the documentation and you will found QbyteArray::toHex().

      QByteArray macAddress = QByteArray::fromHex("123456abcdef");
      macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef"
      macAddress.toHex(0);   // returns "123456abcdef"
    

    If you want to convert a hex string to QByteArray, use QbyteArray::fromHex().

      QByteArray text = QByteArray::fromHex("517420697320677265617421");
      text.data();            // returns "Qt is great!"
    

    To decode binary data from QByteArray, the easiest way it do use QDataStream (see post from @mpergand)

    So what is your problem?



  • @ahsan737
    Then @KroMignon is not the only one who does not understand what you are saying/asking for.

    I have looked at your "attached picture" and I do not understand what it is you want to achieve. You show an "Actual Outcome" which has two windows, one showing some hex numbers and the other the decimal equivalents. You show a "Desired Outcome" which has two lines, one showing hex numbers and the other the decimal equivalents. I don't understand what you want to do to what to achieve what.

    If all you want to do is take either a decimal or a hex string of numbers and output a string in the other base, what is there to say other than you need to convert the input string in the correct base to its number and then convert that to string in the desired output base?

    I do not know what your "I am not able to make changes to the incoming data format. " signifies, there is no need to change incoming format.



  • @KroMignon
    @JonB
    thanks to both of you for paying attention, Now I try to explain at my best.
    Please look at the incoming data pattern in the Desired Outcome, it is starting with "80 0A" but if you look at the Actual Outcome section (green Hex numbers), "80 0A" is the end of the line and then next line starts. So my concern is how can I deal with this situation in order to split data correctly in relevant parts. I hope now I have explained it well.


  • Lifetime Qt Champion

    @ahsan737 You simply start to output the values at the wrong index (see the line "00 00 80 0A"). You need to locate 80 first and start from there.



  • @jsulm the confusion is that it won't be 80 every time, this number will keep updating with every chunk of data.


  • Lifetime Qt Champion

    @ahsan737 Do you have any kind of protocol?! How do you know where a block starts and ends?



  • @jsulm
    CR or LF are not being used for the new line.
    Communication: 9600 baud, 8 data bits, no parity and one stop using Bluetooth (SPP)

    Data Frame:
    Frame.JPG

    is this info sufficient to detect the start of the block?


Log in to reply