比较Qt中的原始十六进制值?

时间:2014-02-06 09:35:34

标签: qt qt4 hex

创建QbyteArray以下,它包含一个十六进制值为49的字节。 49是写入内存的一个字节数据,它不是49的ASCII字符串表示(2字节)

QByteArray data(QByteArray::fromHex("49"));

在某些时候,我用data填充额外的字节。

data.append(QByteArray::fromHex("7656"))

如何将单个字节与十六进制值进行比较?

示例:

此传递(49十六进制为十进制73):

if (data.at(0) == 73) 
qDebug() << "True"

但我需要使用十六进制值:

if (data.at(0) == WHAT_HERE?("49")) 
qDebug() << "True"

2 个答案:

答案 0 :(得分:2)

在C / C ++中,十六进制整数文字以0x为前缀。所以:

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

答案 1 :(得分:1)

如果您在编译时知道它,那么您可以0x49

如果你得到一个QString来与之比较

QString test="47";
bool ok;
int result = test.toInt(&ok,16);
if(ok && data.at(0)==result){
    //they are equal
}