Question

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"
Was it helpful?

Solution

In C/C++, a hexadecimal integer literal is prefixed with 0x. So:

if (data.at(0) == 0x49)
  qDebug() << "true";

OTHER TIPS

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top