The each signed char (a byte) in the array can hold a signed decimal value between -128 and 127. The hex representation of the bits in the byte has no sign, so:
qDebug() << b.toHex();
QByteArray b = "\x7f\xab\xff";
qDebug() << b.toHex();
To copy to clipboard, switch view to plain text mode
Prints "7fabff" as you expect given what we put in the array
// first the signed interpretation of each byte
foreach (char sc, b) {
}
// first the signed interpretation of each byte
foreach (char sc, b) {
qDebug() << QByteArray::number(sc) << QByteArray::number(sc, 16);
}
To copy to clipboard, switch view to plain text mode
"127" "7f"
"-85" "ffffffffffffffab"
"-1" "ffffffffffffffff"
"127" "7f"
"-85" "ffffffffffffffab"
"-1" "ffffffffffffffff"
To copy to clipboard, switch view to plain text mode
The extended hex of the negative numbers is the result of sign extension to a 64-bit int as argument to number().
// and the unsigned version
foreach (char sc, b) {
unsigned char uc = static_cast<unsigned char>(sc);
}
// and the unsigned version
foreach (char sc, b) {
unsigned char uc = static_cast<unsigned char>(sc);
qDebug() << QByteArray::number(uc) << QByteArray::number(uc, 16);
}
To copy to clipboard, switch view to plain text mode
"127" "7f"
"171" "ab"
"255" "ff"
"127" "7f"
"171" "ab"
"255" "ff"
To copy to clipboard, switch view to plain text mode
You see the same bit pattern being interpreted in two different ways through the static_cast.
Bookmarks