How to input hex value to QByteArray efficient ?

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


  • Moderators

    @Hiloshi Using C++11 you can do:

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

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


  • Moderators

    @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

  • @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];

  • 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);

    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)

  • 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())

  • Moderators

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

  • Thank you all.

    if only add CONFIG += c++11:

    QByteArray Testdate{0x41,0x42};
    //then result is

    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.

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

    QByteArray Testdate{0x41,0x42};
    //then result is

    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.

  • Alternatively, you can use QByteArray::fromHex()

    QByteArray header = QByteArray::fromHex("38394201a8");

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.