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 input hex value to QByteArray efficient ?
Forum Updated to NodeBB v4.3 + New Features

How to input hex value to QByteArray efficient ?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 6 Posters 11.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.
  • H Offline
    H Offline
    Hiloshi
    wrote on last edited by
    #1

    Dear All,

    I want to input hex value into QByteArray, below is my code:

     QByteArray header;
     header = header.append(0x38);
     header = header.append(0x39);
     header = header.toHex();
    

    As you see I have to input each value one by one, cannot direct input a series of number.
    For example, I cannot define header[0x38,0x39,0x42,0x01,0xA8] in one code.
    Is my above method correct ? Is there any better way to input hex data directly ?

    Thanks,

    jsulmJ 1 Reply Last reply
    0
    • H Hiloshi

      Dear All,

      I want to input hex value into QByteArray, below is my code:

       QByteArray header;
       header = header.append(0x38);
       header = header.append(0x39);
       header = header.toHex();
      

      As you see I have to input each value one by one, cannot direct input a series of number.
      For example, I cannot define header[0x38,0x39,0x42,0x01,0xA8] in one code.
      Is my above method correct ? Is there any better way to input hex data directly ?

      Thanks,

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Hiloshi Using C++11 you can do:

      QByteArray header{0x38,0x39,0x42,0x01,0xA8};
      

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

      H 1 Reply Last reply
      4
      • jsulmJ jsulm

        @Hiloshi Using C++11 you can do:

        QByteArray header{0x38,0x39,0x42,0x01,0xA8};
        
        H Offline
        H Offline
        Hiloshi
        wrote on last edited by Hiloshi
        #3

        Dear @jsulm ,

        It is not working in my project, I suspect my project is not C++ 11 ??
        The error message is:

        no matching function for call to ‘QByteArray::QByteArray(<brace-enclosed initializer list>)’
             QByteArray header{0x38,0x39,0x42,0x01,0xA8};
                                                       ^
        

        My compiler is "~/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++". Is it support C++ 11 ?

        Thanks,

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • H Hiloshi

          Dear @jsulm ,

          It is not working in my project, I suspect my project is not C++ 11 ??
          The error message is:

          no matching function for call to ‘QByteArray::QByteArray(<brace-enclosed initializer list>)’
               QByteArray header{0x38,0x39,0x42,0x01,0xA8};
                                                         ^
          

          My compiler is "~/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++". Is it support C++ 11 ?

          Thanks,

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Hiloshi Please call your compiler with -v parameter to get the version.
          Also you probably have to activate C++11 first in pro file:

          CONFIG += c++11
          

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

          1 Reply Last reply
          3
          • H Hiloshi

            Dear @jsulm ,

            It is not working in my project, I suspect my project is not C++ 11 ??
            The error message is:

            no matching function for call to ‘QByteArray::QByteArray(<brace-enclosed initializer list>)’
                 QByteArray header{0x38,0x39,0x42,0x01,0xA8};
                                                           ^
            

            My compiler is "~/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++". Is it support C++ 11 ?

            Thanks,

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Hiloshi I don't think you can initialize a QBytrArray in that way.

            this however should work:

            QList<int> l{0xFE,0x64,0x01};
            QByteArray bArray;
            for(int i =0; i< l.size(); i++)
                bArray[i] = l[i];
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            jsulmJ 1 Reply Last reply
            1
            • M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #6

              C++11 you can do:
              QByteArray header{0x38,0x39,0x42,0x01,0xA8};

              No, it's not working in C++14 either.

              This seems to work:

              unsigned char dat[]={0x38,0x39,0x42,0x01,0xA8};
              QByteArray hex=QByteArray::fromRawData((char*)dat,5);
              qDebug()<<hex.toHex();
              

              Annoying error with unsigned char, requiring a cast ...
              (it's clearly explained in the doc, but they use const char that produce an error for me)

              1 Reply Last reply
              3
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by A Former User
                #7

                You can also follow one of these three ways:

                QByteArray ba1{"\x38\x39\x42\x01\xA8"}; 
                QByteArray ba2 = QByteArrayLiteral("\x38\x39\x42\x01\xA8");
                Stuff::ByteArray ba3{0x38,0x39,0x42,0x01,0xA8};
                

                ba1 is constructed from a C-style string literal using QByteArray::QByteArray(const char *data, int size = -1). ba2 is probably the most efficient, see QStringLiteral explained and Qt Weekly #13: QStringLiteral. For ba3 we use a small helper class that extends QByteArray:

                namespace Stuff
                {
                    struct ByteArray: public QByteArray
                    {
                        ByteArray(std::initializer_list<unsigned char> lst)
                            : QByteArray(reinterpret_cast<char const *>(lst.begin()), lst.size())
                        {}
                    };
                }
                
                1 Reply Last reply
                5
                • J.HilkJ J.Hilk

                  @Hiloshi I don't think you can initialize a QBytrArray in that way.

                  this however should work:

                  QList<int> l{0xFE,0x64,0x01};
                  QByteArray bArray;
                  for(int i =0; i< l.size(); i++)
                      bArray[i] = l[i];
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @J.Hilk @mpergand Yes, looks like it is not supported

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

                  1 Reply Last reply
                  1
                  • H Offline
                    H Offline
                    Hiloshi
                    wrote on last edited by
                    #9

                    Thank you all.

                    if only add CONFIG += c++11:

                    QByteArray Testdate{0x41,0x42};
                    qDebug()<<"Testdata="<<Testdate;
                    //then result is
                    Testdata= "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
                    

                    Use QList, QByteArray::fromRawData and @Wieland 's three approach could input hex value to QByteArray successfully.

                    I should try to understand what is C++11 and read Qt Weekly #13: QStringLiteral in more detail.

                    Thanks again.

                    ? 1 Reply Last reply
                    0
                    • H Hiloshi

                      Thank you all.

                      if only add CONFIG += c++11:

                      QByteArray Testdate{0x41,0x42};
                      qDebug()<<"Testdata="<<Testdate;
                      //then result is
                      Testdata= "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
                      

                      Use QList, QByteArray::fromRawData and @Wieland 's three approach could input hex value to QByteArray successfully.

                      I should try to understand what is C++11 and read Qt Weekly #13: QStringLiteral in more detail.

                      Thanks again.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by A Former User
                      #10

                      @Hiloshi said in How to input hex value to QByteArray efficient ?:

                      QByteArray Testdate{0x41,0x42};
                      qDebug()<<"Testdata="<<Testdate;
                      //then result is
                      Testdata= "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"

                      Because QByteArray Testdate{0x41,0x42}; calls this constructor: QByteArray::QByteArray(int size, char ch) with size=0x41=65 and ch=0x42='B'.

                      See QByteArray Class:

                      Constructs a byte array of size size with every byte set to character ch.

                      1 Reply Last reply
                      3
                      • oPryzeLPO Offline
                        oPryzeLPO Offline
                        oPryzeLP
                        wrote on last edited by
                        #11

                        Alternatively, you can use QByteArray::fromHex()

                        QByteArray header = QByteArray::fromHex("38394201a8");
                        
                        1 Reply Last reply
                        5

                        • Login

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