how to convert qbytearray to hex array
-
-
such as :
qbytearray test;
test[0] = 0x30;
test[1] = 0x31;
qdebug << "test[0] = "<< test[0];
qdebug << "test[1] = "<< test[1];
i want qdebug output:
test[0] = 0x30;
test[1] = 0x31;
not
test[0] = "0x30" or test[0] = "0"
what should i do to get this result?@Liny said in how to convert qbytearray to hex array:
test[0] = 0x30;
0x30 is a string representation of a binary value in hexadecimal.
[EDIT]
QByteArray is an array of char.
So test[31] is printed as ascii char 1To print hex values you can do:
qdebug << hex << "test[1] = "<< test[1]; -
@Liny said in how to convert qbytearray to hex array:
test[0] = 0x30;
0x30 is a string representation of a binary value in hexadecimal.
[EDIT]
QByteArray is an array of char.
So test[31] is printed as ascii char 1To print hex values you can do:
qdebug << hex << "test[1] = "<< test[1]; -
@mpergand
i try this :
qbytearray test;
test[0] = 0x30;
test[1] = 0x31;
qdebug <<"test[0] = "<< hex << test[0];
qdebug <<"test[1] = "<< hex << test[1];
but i get test[0] = 0,test[1] = 1;
why? -
@mpergand
i try this :
qbytearray test;
test[0] = 0x30;
test[1] = 0x31;
qdebug <<"test[0] = "<< hex << test[0];
qdebug <<"test[1] = "<< hex << test[1];
but i get test[0] = 0,test[1] = 1;
why?@Liny
I forgot the cast !
qDebug() <<hex<< "test[1] = "<< (uint8_t)test[1];Note the unsigned int.
casting to int, uint or uint8_t don't print the same result:test[0] = 0x90; qDebug() <<hex<< "test[0] = "<< (int)test[0] << (uint)test[0]<< (uint8_t)test[0];
test[0] = -70 ffffff90 90
-
@Liny
I forgot the cast !
qDebug() <<hex<< "test[1] = "<< (uint8_t)test[1];Note the unsigned int.
casting to int, uint or uint8_t don't print the same result:test[0] = 0x90; qDebug() <<hex<< "test[0] = "<< (int)test[0] << (uint)test[0]<< (uint8_t)test[0];
test[0] = -70 ffffff90 90
-
@mpergand said in how to convert qbytearray to hex array:
(uint8_t)test[0]
Why the cast, and why does that change it? I haven't looked, but isn't
QByteArray
base element typeuint8_t
, or similar? -
char at(int i) const
char operator[](int i) constthey return char, so debug prints ascii char, hence the cast if you want an other type.
-