Solved Converting QByteArray to unsigned char
-
Hi all
I have a populated QByteArray - filled with hexadecimal numbers (0x01, 0x41 etc.).
I need to convert this to a unsigned char array of the same size. Any Ideas?
QByteArray data; data[0] = 0x41; data[1] = 0x12; //Append ff codes to datastream (representing no change) for (int i = 0; i < 35; i++) { data.append(0xFF); } int count = data.size(); unsigned char hex[count];
How do I populate the unsigned char hex with the contents of the QByteArray data?
Thanks
James
-
unsigned char hex[count];
is invalid in C++, it only compiles if your compiler treats it as C, as far as I know only gcc does that by defaultconst std::size_t count = data.size(); unsigned char* hex =new unsigned char[count]; std::memcpy(hex,data.constData(),count);
-
@VRonin I get this error with that code:
C:\PMPS\PMPSv1\pmps_f.cpp:1605: error: reinterpret_cast from type 'const char*' to type 'unsigned char*' casts away qualifiers
memcpy(hex,reinterpret_cast<unsigned char*>(data.constData()),count);
^ -
@James-Sprinks I ninja'd in my correction. it was easy btw
-
@VRonin So what is the correction?
-
@VRonin said in Converting QByteArray to unsigned char:
std::memcpy(hex,data.constData(),count);
std::memcpy(hex,data.constData(),count);
as written above -
@VRonin Brilliant. Thank you!
-
On a side note, you could also use
reinterpret_cast<unsigned char*>(data.data())
if you don't need to copy it to a separate array -
Sorry for hijacking, but what do you guys think of QTBUG-64746?
It seems very common to use QByteArray as a raw byte container and sometimes you need
unsigned char
insteadchar
. -
@aha_1980 I wouldn't be averse to it
I have a case were I check specific bytes for values first, before managing the whole byte array and some checks are higher than 0x7F.
The comparingoperator ==(0xAA)
works fine, but is always taged with a warning. Which tickles my OCD.To your point made in the Bugreport-ticket.
In my current case, a QtSerialBus communication, I combine 2 chars to a short via QDatatStream.
{//section from my class ... QDataStream out(m_data); quint8 countData; QVector<quint16> values; out >> countData; countData = countData/2; quint16 value; for(int i(0); i <countData; i++){ out >> value; if(out.status() == QDataStream::Ok) values.append(value); } return values; }
I did this because I didn't want to use all the reinterpret_cast's. Tried it first, became unreadable pretty quickly x)
-
@J.Hilk said in Converting QByteArray to unsigned char:
I did this because I didn't want to use all the reinterpret_cast's. Tried it first, became unreadable pretty quickly x)
That's my motivation also. Of course QDataStream is the correct solution, but that's still a lot of code lines for a simple problem. And sometimes you just want to create a QByteArray with some values in it, which should be a one-liner IMHO.
-
@aha_1980 said in Converting QByteArray to unsigned char:
And sometimes you just want to create a QByteArray with some values in it, which should be a one-liner IMHO.
verry true,
I'm missing a simple constructor/initalizer list functionalityfor example:
//I wish this was possible QByteArray ba{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
-
Try the macro QByteArrayLiteral
https://doc.qt.io/qt-5/qbytearray.html#QByteArrayLiteral
QByteArray b = QByteArrayLiteral("\x12\x00\xa4\x42\x51\x00\x00\x99");