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 35.9k Views 1 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.
  • SGaistS SGaist

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

    kahlenbergK Offline
    kahlenbergK Offline
    kahlenberg
    wrote on last edited by
    #5

    @SGaist QString MyHexString = "E00200BA9EFC00AC";

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

      kahlenbergK 1 Reply Last reply
      2
      • SGaistS SGaist

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

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

        ?

        kahlenbergK Offline
        kahlenbergK Offline
        kahlenberg
        wrote on 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);
        ^

        KroMignonK 1 Reply Last reply
        0
        • kahlenbergK kahlenberg

          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 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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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

            kahlenbergK 1 Reply Last reply
            4
            • SGaistS SGaist

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

              kahlenbergK Offline
              kahlenbergK Offline
              kahlenberg
              wrote on 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
              0
              • kahlenbergK kahlenberg

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

                R Offline
                R Offline
                rmk1842
                wrote on last edited by
                #11

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

                1 Reply Last reply
                0
                • kahlenbergK kahlenberg

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

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on 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 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 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
                      0
                      • M mganesh
                              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 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