Compare raw hexadecimal values in Qt? - qt

Below a QbyteArray is created and it contains a byte with the hexadecimal value 49. 49 is one byte of data written in memory, it isn't an ASCII string representation of 49 (which is 2 bytes)
QByteArray data(QByteArray::fromHex("49"));
At some point I populate the data with additional bytes.
data.append(QByteArray::fromHex("7656"))
How to compare a single byte with a hex value?
Example:
This passes (49 hex is 73 decimal):
if (data.at(0) == 73)
qDebug() << "True"
But i need to work with hex values:
if (data.at(0) == WHAT_HERE?("49"))
qDebug() << "True"

In C/C++, a hexadecimal integer literal is prefixed with 0x. So:
if (data.at(0) == 0x49)
qDebug() << "true";

if you know it at compile time then you can do 0x49
if you get a QString to compare it with then
QString test="47";
bool ok;
int result = test.toInt(&ok,16);
if(ok && data.at(0)==result){
//they are equal
}

Related

QBuffer writes bytes at the start of QByteArray rather than the end

I have the following code:
QByteArray ba; // Declare Byte array
ba.clear(); // Clear it
ba.append(80, 0x00); // Append 80 bytes of 0x00
quint32 Count = 2; // The number we want to append to the byte array
QBuffer tempBuffer(&ba); // We use temporary buffer to conveniently put integers and floats into byte-array
tempBuffer.open(QIODevice::WriteOnly);
Count = qToLittleEndian(Count); // Make sure our number is little Endian
tempBuffer.write((char*)&Count, sizeof(quint32)); // Write the number to byte array
When I print to console the content of my byte array:
qDebug() << "ba: " << ba.toHex();
The console prints:
ba: "0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
As can be seen above, the 2 which is of type quint32, is correctly represented by the little Endian hex value of 0x02000000, however, it is added at the start of the byte array rather than the end. How can I append my value to the end of the byte array?
Open the buffer in append mode instead of writeonly:
tempBuffer.open(QIODevice::Append);

How can i convert a QByteArray into a hex string?

I have the blow QByteArray.
QByteArray ba;
ba[0] = 0x01;
ba[1] = 0x10;
ba[2] = 0x00;
ba[3] = 0x07;
I have really no idea how to convert this QByteArray into resulted string which have "01100007", which i would use the QRegExp for pattern matching on this string?
First of all, the QByteArray does not contain "hex values", it contains bytes (as it's name implies). Number can be "hex" only when it is printed as text.
Your code should be:
QByteArray ba(4, 0); // array length 4, filled with 0
ba[0] = 0x01;
ba[1] = 0x10;
ba[2] = 0x00;
ba[3] = 0x07;
Anyway, to convert a QByteArray to a hex string, you got lucky: just use QByteArray::toHex() method!
QByteArray ba_as_hex_string = ba.toHex();
Note that it returns 8-bit text, but you can just assign it to a QString without worrying much about encodings, since it is pure ASCII. If you want upper case A-F in your hexadecimal numbers instead of the default a-f, you can use QByteArray::toUpper() to convert the case.
QString has following contructor:
constructor QString(const QByteArray &ba)
But note that an octal number is preceeded by 0 in c++, so some of your values are deciamal, some octal, none of them are hex.

Construct a QByteArray from a HEX value entered as a QString

If QString str = "0xFFFF", how to turn this text representation of an hex into a QByteArray? In the end, I would like to have the same of what I get from the following:
QByteArray ba;
ba.resize(2);
ba[0] = 0xFF;
ba[1] = 0xFF;
In either case the final QByteArray would be a sequence of hex values, i.e. FFFF. Conversion should be applied on that string. Given that, if your input string is provided prepended with the 0x, you should get rid of it via mid().
Here is a code snippet which compares the results of the two approaches: manually filling the QByteArray with hex values or converting hex values from a QString:
QByteArray array1;
array1.resize(2);
array1[0] = 0xFF;
array1[1] = 0xFF;
QString str = "0xFFFF";
QString value = str.mid(2); // "FFFF" <- just the hex values!
QByteArray array2 = QByteArray::fromHex(value.toLatin1());
qDebug() << array1; // not printable chars
qDebug() << array2;
qDebug() << array1.toHex(); // to have a printable form use "toHex()"!
qDebug() << array2.toHex();

Get a string representation of a single byte from QByteArray?

I have a QByteArray i create manually:
QByteArray hexArray(QByteArray::fromHex("495676"));
If this was encoded ASCII it would be "IVv".
If I want to get a single byte of data from that array.
I can do that like this:
qDebug() << messageToBeSent_raw[0];
However, that outputs I, which is correct but I would like to get 49. What I'm looking for is an equivalent of the QByteArray::toHex() just for a single byte. Is there a way to do it?
You can use QString::number.
qDebug() << QString::number(hexArray[0], 16);

How can I convert integer to qbitarray?

How can I get QBitArray from qint value? I need to change some bits in number, so I want to use QBitArray for it.
QBitArray's '>>' operator expects bitset size as the first 4 bytes, so JustMaximumPower's snipper won't work. Correct data stream should look like this:
QBitArray bits;
quint32 size = 32;
quint32 value = 1337;
QByteArray data;
QDataStream stream(data, QIODevice::ReadWrite);
stream << size << value;
stream.device()->seek(0);
stream >> bits;
Actually I don't find QBitArray very useful. You could try std::vector<bool> for variable size or std::bitset for fixed size (both from STL library).
std::bitset<32> bits(1337);
bits[0] = 1;
Not testet but should work:
qint value = 1337;
QBitArray bits;
QDataStream stream;
stream << value;
bits << stream;
If you want to just change some bits, QBitArray is overkill.
int bitmask = 1 << 3; // let's change 4th bit
bitmask = 0x55555555; // or perhaps all odd bits
int number = 12345678;
number |= bitmask; // set to 1s
number &= ~bitmask; // set to 0s
number ^= bitmask; // negate what's already there

Resources