Solved How can I convert the data in QByteArray which use hex to express the complement.
-
Now, I get some QByteArray which use hex to express the complement. For example, QByteArray data1="01" means 00000001 equal to 1, QByteArray data2="0f" means 00001111 equal to 15, QByteArray data2="ff" means 11111111 equal to -1.
How can i convert the data(QByteArray type) to int, i have to write a function to manually complete the conversion? Or Qt provides some way to complete this conversion?
Thanks
-
You obtain the complete QByteArray represented as hex with toHex.
There are other helpful conversion around there. -
So basically you have a two-character string that you want to convert to a signed byte and then to int.
This will do it:QByteArray data1 = "0F"; int data1int = static_cast<qint8>(data1.toInt(nullptr, 16)); QByteArray data2 = "FF"; int data2int = static_cast<qint8>(data2.toInt(nullptr, 16)); qDebug() << data1int << data2int; //prints 15 -1
Unfortunately QByteArray does not have
toInt8()
ortoChar()
method so you need to go through one extra conversion (string -> int -> int8 -> int). If it had it would just be (string -> int8 ->int). -
This post is deleted! -
@Chris-Kawa Thank you very very much! It is want i want!