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?
Forum Updated to NodeBB v4.3 + New Features

How to Split Hex String?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 7 Posters 2.9k Views 2 Watching
  • 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.
  • ahsan737A ahsan737

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

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

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

    ahsan737A 1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on 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 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

        ahsan737A 1 Reply Last reply
        2
        • M mpergand

          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

          ahsan737A Offline
          ahsan737A Offline
          ahsan737
          wrote on 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
          • JonBJ JonB

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

            ahsan737A Offline
            ahsan737A Offline
            ahsan737
            wrote on 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.

            KroMignonK 1 Reply Last reply
            0
            • ahsan737A ahsan737

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

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on 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)

              ahsan737A 1 Reply Last reply
              3
              • KroMignonK KroMignon

                @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.
                ahsan737A Offline
                ahsan737A Offline
                ahsan737
                wrote on 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)

                KroMignonK JonBJ 2 Replies Last reply
                0
                • ahsan737A ahsan737

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

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on 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
                  • ahsan737A ahsan737

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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.

                    ahsan737A 1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      ahsan737A Offline
                      ahsan737A Offline
                      ahsan737
                      wrote on 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.

                      jsulmJ 1 Reply Last reply
                      0
                      • ahsan737A ahsan737

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

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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

                        ahsan737A 1 Reply Last reply
                        2
                        • jsulmJ jsulm

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

                          ahsan737A Offline
                          ahsan737A Offline
                          ahsan737
                          wrote on 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.

                          jsulmJ 1 Reply Last reply
                          0
                          • ahsan737A ahsan737

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

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 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

                            ahsan737A 1 Reply Last reply
                            2
                            • jsulmJ jsulm

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

                              ahsan737A Offline
                              ahsan737A Offline
                              ahsan737
                              wrote on 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

                              • Login

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