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. How to Split Hex String?

How to Split Hex String?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 7 Posters 2.7k 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.
  • A ahsan737
    18 May 2020, 08:53

    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.

    J Online
    J Online
    JonB
    wrote on 18 May 2020, 08:58 last edited by
    #3

    @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.

    A 1 Reply Last reply 18 May 2020, 09:20
    0
    • A Offline
      A Offline
      ahsan737
      wrote on 18 May 2020, 08:59 last edited by
      #4

      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.

      1 Reply Last reply
      0
      • J JonB
        18 May 2020, 08:58

        @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.

        A Offline
        A Offline
        ahsan737
        wrote on 18 May 2020, 09:20 last edited by
        #5

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

        J 1 Reply Last reply 18 May 2020, 10:47
        0
        • A ahsan737
          18 May 2020, 09:20

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

          J Online
          J Online
          JonB
          wrote on 18 May 2020, 10:47 last edited by
          #6

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

          A 1 Reply Last reply 19 May 2020, 07:33
          0
          • F Offline
            F Offline
            fcarney
            wrote on 18 May 2020, 15:22 last edited by
            #7

            Maybe show us some code of what you tried?

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on 18 May 2020, 16:25 last edited by
              #8

              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

              A 1 Reply Last reply 19 May 2020, 07:25
              2
              • M mpergand
                18 May 2020, 16:25

                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

                A Offline
                A Offline
                ahsan737
                wrote on 19 May 2020, 07:25 last edited by ahsan737
                #9

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

                1 Reply Last reply
                0
                • J JonB
                  18 May 2020, 10:47

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

                  A Offline
                  A Offline
                  ahsan737
                  wrote on 19 May 2020, 07:33 last edited by
                  #10

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

                  K 1 Reply Last reply 19 May 2020, 07:40
                  0
                  • A ahsan737
                    19 May 2020, 07:33

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

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 19 May 2020, 07:40 last edited by
                    #11

                    @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.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    A 1 Reply Last reply 19 May 2020, 07:59
                    3
                    • K KroMignon
                      19 May 2020, 07:40

                      @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.
                      A Offline
                      A Offline
                      ahsan737
                      wrote on 19 May 2020, 07:59 last edited by ahsan737
                      #12

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

                      K J 2 Replies Last reply 19 May 2020, 08:33
                      0
                      • A ahsan737
                        19 May 2020, 07:59

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

                        K Offline
                        K Offline
                        KroMignon
                        wrote on 19 May 2020, 08:33 last edited by KroMignon
                        #13

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

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        1 Reply Last reply
                        0
                        • A ahsan737
                          19 May 2020, 07:59

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

                          J Online
                          J Online
                          JonB
                          wrote on 19 May 2020, 08:35 last edited by
                          #14

                          @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.

                          A 1 Reply Last reply 19 May 2020, 08:46
                          0
                          • J JonB
                            19 May 2020, 08:35

                            @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.

                            A Offline
                            A Offline
                            ahsan737
                            wrote on 19 May 2020, 08:46 last edited by
                            #15

                            @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.

                            J 1 Reply Last reply 19 May 2020, 09:02
                            0
                            • A ahsan737
                              19 May 2020, 08:46

                              @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.

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 19 May 2020, 09:02 last edited by
                              #16

                              @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.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              A 1 Reply Last reply 19 May 2020, 09:09
                              2
                              • J jsulm
                                19 May 2020, 09:02

                                @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.

                                A Offline
                                A Offline
                                ahsan737
                                wrote on 19 May 2020, 09:09 last edited by
                                #17

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

                                J 1 Reply Last reply 19 May 2020, 09:13
                                0
                                • A ahsan737
                                  19 May 2020, 09:09

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

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 19 May 2020, 09:13 last edited by
                                  #18

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

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  A 1 Reply Last reply 20 May 2020, 04:36
                                  2
                                  • J jsulm
                                    19 May 2020, 09:13

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

                                    A Offline
                                    A Offline
                                    ahsan737
                                    wrote on 20 May 2020, 04:36 last edited by ahsan737
                                    #19

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

                                    1 Reply Last reply
                                    0

                                    12/19

                                    19 May 2020, 07:59

                                    • Login

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