Unsolved Convert unsigned char to QString
-
Hello
Looking for some help. I am trying to convert an unsigned char array to a string or QString. I have tried a few different ways so far with no success or crashing the application when testing.
The char array need into a string is buf[8] + buf[9] and these contain hex codes (ie f8 + 6f). So I want my string to read "f86f". I didn't think it would be as difficult as it is as it is no problem printing f8 and 6f to the console!
Thanks in advance!
-
Try something like this:
const QByteArray data = QByteArray::number(buf); const QString string = data.toHex();
brain to terminal, not tested; you'll probably need to tweak it.
-
@aftershocker1 said in Convert unsigned char to QString:
The char array need into a string is buf[8] + buf[9] and these contain hex codes (ie f8 + 6f). So I want my string to read "f86f".
If you really mean this --- the string is to read
f86f
, i.e. showing the characters/bytes in hex --- you will want to useQString::toLatin1().toHex()
. -
I don't think I understood the problem properly in my initial post. The value in buf[9] is 248 (0xF8) and the value in buf[10] is 111 (0x6f) although these values may change(data is coming in from a hid).
In the console I can get it to print the value in hex format using
qDebug("%02hhx ", buf[9]); qDebug("%02hhx ", buf[10]);
So, what I want to do is use the hex values and turn them into a string(once in a string I have a function to convert the string into binary which enables me to see the error flags from the hid).
What is the best way to convert buf[9] and buf[10] into the hex numbers and change them into a string?
-
@aftershocker1 said in Convert unsigned char to QString:
What is the best way to convert buf[9] and buf[10] into the hex numbers and change them into a string?
That's what we answered above already.
But isn't it wasteful for you to convert a number (which is already binary!) to string, then to hex, then to binary again? Why don't you just work on the numbers directly?
-
@aftershocker1
I already suggestedQString::toLatin1().toHex()
. Without bothering with aQString
why not go straight from aQByteArray
(you have bytes) via QByteArray QByteArray::toHex() const? -
@sierdzio Good point! I have just got a little lost in the process.
-
So the code below why would data = 248 (which is the correct value of buf[9]) and data_as_hex_string = 323438?
QByteArray data = QByteArray::number(buf[9]); QString data_as_hex_string = data.toHex();
I thought the data_as_hex would be F8?
-
@aftershocker1
My friend, you really need to sort your understanding of bytes, strings & numbers out! :)why would data = 248
No!
data
is set to"248"
, a string, not a number. Look at https://doc.qt.io/qt-5/qbytearray.html#numberReturns a byte array containing the string equivalent of the number n to base base (10 by default).
So the string is 3 characters, and the hex string representation of those 3 bytes is
"323438"
.You want something more like:
QByteArray data(buf, sizeOfBuffer); // wrap buffer (or some section of it) in a `QByteArray` QString data_as_hex_string = data.mid(9, 2).toHex(); // pick out bytes [9,10] for conversion to hex