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 convert ASCII Hex string to Byte array?

How to convert ASCII Hex string to Byte array?

Scheduled Pinned Locked Moved General and Desktop
hexasciibytearray
15 Posts 8 Posters 33.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.
  • S SGaist
    26 Nov 2015, 22:18

    Hi,

    Do you mean something like QByteArray::fromHex ?

    K Offline
    K Offline
    kahlenberg
    wrote on 27 Nov 2015, 08:34 last edited by
    #3

    @SGaist Thanks for answer, almost that. But it takes a QByteArray as parameter. I have a string. How can I convert a string to a QByteArray ? Every two character in my string represents a byte.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 27 Nov 2015, 08:39 last edited by
      #4

      Do you mean a std::string or "MyHexString" ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply 27 Nov 2015, 08:56
      0
      • S SGaist
        27 Nov 2015, 08:39

        Do you mean a std::string or "MyHexString" ?

        K Offline
        K Offline
        kahlenberg
        wrote on 27 Nov 2015, 08:56 last edited by
        #5

        @SGaist QString MyHexString = "E00200BA9EFC00AC";

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 27 Nov 2015, 09:02 last edited by
          #6

          Then why not do as the example in the doc suggests:

          QByteArray myHexArray = QByteArray::fromHex("E00200BA9EFC00AC");
          

          ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply 27 Nov 2015, 09:34
          2
          • S SGaist
            27 Nov 2015, 09:02

            Then why not do as the example in the doc suggests:

            QByteArray myHexArray = QByteArray::fromHex("E00200BA9EFC00AC");
            

            ?

            K Offline
            K Offline
            kahlenberg
            wrote on 27 Nov 2015, 09:34 last edited by
            #7

            @SGaist
            Becausue fromHex accepts QByteArray as parameter, I have QString. Later I will get this string from UI element, QLineEdit
            (ui->cmdToWrite->text(), this is QString)

            QString MyHexString = "E00200BA9EFC00AC";
            QByteArray cmd = QByteArray::fromHex(MyHexString);
            

            Error message:
            D:\Qt\Projects\Widget\mainwindow.cpp:40: error: no matching function for call to 'QByteArray::fromHex(QString&)'
            QByteArray cmd = QByteArray::fromHex(MyHexString);
            ^

            K 1 Reply Last reply 20 Aug 2020, 14:20
            0
            • K kahlenberg
              26 Nov 2015, 22:16

              Hi,
              I try to convert a string, containing hex chars only, to a byte array. I could do it in C but in Qt I did not find any solution :) . Yes it is easy and trivial but really I need help.

              Lets say I have a string like "E00200BA9EFC00AC", how can I convert it to an array like this:

              bytearray[0]=0xE0
              bytearray[1]=0x02
              bytearray[2]=0x00
              bytearray[3]=0xBA
              ....
              bytearray[7]=0xAC

              J Offline
              J Offline
              jduran_gm
              wrote on 27 Nov 2015, 09:40 last edited by
              #8

              @kahlenberg Take a look to this member function:

              QByteArray QByteArray::fromHex(const QByteArray & hexEncoded)

              Joaquim Duran

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 27 Nov 2015, 09:43 last edited by
                #9

                Again, why create a QString ? Just use QByteArray directly or if you really really want a QString, use e.g. toLatin1

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                K 1 Reply Last reply 27 Nov 2015, 11:14
                4
                • S SGaist
                  27 Nov 2015, 09:43

                  Again, why create a QString ? Just use QByteArray directly or if you really really want a QString, use e.g. toLatin1

                  K Offline
                  K Offline
                  kahlenberg
                  wrote on 27 Nov 2015, 11:14 last edited by
                  #10

                  @SGaist Oh, thank you very much, toLatin1() solved my problem. I didn't see it :) Thanks again.

                  R 1 Reply Last reply 19 Aug 2020, 19:45
                  0
                  • K kahlenberg
                    27 Nov 2015, 11:14

                    @SGaist Oh, thank you very much, toLatin1() solved my problem. I didn't see it :) Thanks again.

                    R Offline
                    R Offline
                    rmk1842
                    wrote on 19 Aug 2020, 19:45 last edited by
                    #11

                    @kahlenberg : could you post the code? I am in the same situation.

                    1 Reply Last reply
                    0
                    • K kahlenberg
                      27 Nov 2015, 09:34

                      @SGaist
                      Becausue fromHex accepts QByteArray as parameter, I have QString. Later I will get this string from UI element, QLineEdit
                      (ui->cmdToWrite->text(), this is QString)

                      QString MyHexString = "E00200BA9EFC00AC";
                      QByteArray cmd = QByteArray::fromHex(MyHexString);
                      

                      Error message:
                      D:\Qt\Projects\Widget\mainwindow.cpp:40: error: no matching function for call to 'QByteArray::fromHex(QString&)'
                      QByteArray cmd = QByteArray::fromHex(MyHexString);
                      ^

                      K Offline
                      K Offline
                      KroMignon
                      wrote on 20 Aug 2020, 14:20 last edited by KroMignon
                      #12

                      @rmk1842

                      QString MyHexString = "E00200BA9EFC00AC";
                      QByteArray cmd = QByteArray::fromHex(MyHexString);

                      You simply have to use QString::toLatin1():

                      QString MyHexString = "E00200BA9EFC00AC";
                      QByteArray cmd = QByteArray::fromHex(MyHexString.toLatin1());
                      

                      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
                      • fcarneyF Offline
                        fcarneyF Offline
                        fcarney
                        wrote on 20 Aug 2020, 14:26 last edited by fcarney
                        #13

                        Like @SGaist said.
                        But QByteArray takes a string constant. I am using it is code right now. Extensively.

                        C++ is a perfectly valid school of magic.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mganesh
                          wrote on 22 Mar 2021, 11:24 last edited by
                          #14
                                monitorCommand = "020400000006703B";
                              QByteArray data_to_transmit = QByteArray::fromHex(monitorCommand.toUtf8());
                          

                          qDebug() << monitorCommand << data_to_transmit;

                          it gives "020400000006703B for monitorcommond
                          for data_to_transmit "\x02\x04\x00\f\x00\x06\xB0""8"
                          but its not correctly converted to hex

                          Christian EhrlicherC 1 Reply Last reply 22 Mar 2021, 12:00
                          0
                          • M mganesh
                            22 Mar 2021, 11:24
                                  monitorCommand = "020400000006703B";
                                QByteArray data_to_transmit = QByteArray::fromHex(monitorCommand.toUtf8());
                            

                            qDebug() << monitorCommand << data_to_transmit;

                            it gives "020400000006703B for monitorcommond
                            for data_to_transmit "\x02\x04\x00\f\x00\x06\xB0""8"
                            but its not correctly converted to hex

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 22 Mar 2021, 12:00 last edited by
                            #15

                            @mganesh This works fine for me. Your output can not match on what you've written. Please post all your test code

                            int main(int argc, char **argv)
                            {
                              QCoreApplication app(argc, argv);
                              QString monitorCommand = "020400000006703B";
                              QByteArray data_to_transmit = QByteArray::fromHex(monitorCommand.toUtf8());
                              qDebug() << monitorCommand << data_to_transmit;
                              return 0;
                            }
                            

                            -->
                            "020400000006703B" "\x02\x04\x00\x00\x00\x06p;"

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            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