在qt中将十六进制转换为二进制

converting hex into binary in qt

我必须从 MODbus 应用程序读取原始数据并将其转换为十六进制。在 MODbus 中,它以二进制和十六进制显示。可以说我想读取 5 位 = 00011 并且当我将它转换为十六进制时它变成 0x03 但是当我再次将它从十六进制转换为二进制时。它只在我的UI中显示= 11,剩下的3个零000消失了。这是我的编码:

if (my_arguments.num_of_points <=8) 
{
    for(int x = 0; x < 1; x++)
    {
        block33.append(block22[9+x]);
    }
}

hexVal = block33.toHex(); //for converting to binary
stringValue = "0x " + block33.toHex().toUpper();  //display hex raw value

QString hexadecimalNumber = hexVal;

bool ok = false;

QString binaryNumber = QString::number(hexadecimalNumber.toLongLong(&ok, 16),2);

ui->textEdit->append(binaryNumber);

ui->textEdit->setText("RawValue in Hex = " + hex_rawValue );

输出=

  RawValue in Hex = 03

   11                  //but i read 5 bits so it should show `00011

使用 QString::arg() 重载而不是 QString::number()。它有一个 fieldWidth 参数,您可以用它来填充添加的零:

bool ok;
QString hexString = "0x03";
qDebug() << "BINARY 1: " << QString::number(hexString.toLongLong(&ok, 16),2);
qDebug() << "BINARY 2: " << QString("%1").arg(hexString.toULongLong(&ok, 16), 5, 2, QChar('0'));

输出为:

BINARY 1:  "11"
BINARY 2:  "00011"

所以根据 QString::arg() 上的文档:

QString QString::arg(qulonglong a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const

fieldWidth specifies the minimum amount of space that a is padded to and filled with the character fillChar. A positive value produces right-aligned text; a negative value produces left-aligned text.

The base argument specifies the base to use when converting the integer a into a string. base must be between 2 and 36, with 8 giving octal, 10 decimal, and 16 hexadecimal numbers.

If fillChar is '0' (the number 0, ASCII 48), the locale's zero is used. For negative numbers, zero padding might appear before the minus sign.

因此

  1. 5 指出生成的字符串应至少包含 5 个字符。
  2. 2 指出输出的基数应该是 2(二进制)
  3. QChar('0') 表示如果结果少于 5 个字符,则填充 0 个字符,直到结果字符串中有 5 个字符。

因为5是正数,所以前面加了0个字符。如果它是 -5(负数),字符将在后面填充。

例如,如果您有 10 位,鉴于上述情况,将不会进行任何填充,因为它超过了指定的 5 位。

.

后面不清楚的地方我不明白

如果它在您的情况下不起作用,则其他地方做错了。 (这是其他评论者已经告诉你的。)

我怎么说服你?

我最后一次(绝望的)尝试:一次(不是这样)MCVE:

#include <QDebug>
#include <QString>

int main()
{
  QString samples[] = {
    "0x0", "0x1", "0x1e", "0x11", "0x1111", "0x111e", "0x111f"
  };
  enum { n = sizeof samples / sizeof *samples };
  for (int i = 0; i < n; ++i) {
    const QString &hexString = samples[i];
    qDebug() << "Formatted binary output of " << hexString;
    for (int j = 1; j < 40; j += 8) {
      bool ok;
      qDebug()
        << QString("%1: %2")
        .arg(j, 2)
        .arg(hexString.toULongLong(&ok, 16), j, 2, QChar('0'));
    }
  }
  // done
  return 0;
}

编译测试:

Formatted binary output of  "0x0"
" 1: 0"
" 9: 000000000"
"17: 00000000000000000"
"25: 0000000000000000000000000"
"33: 000000000000000000000000000000000"
Formatted binary output of  "0x1"
" 1: 1"
" 9: 000000001"
"17: 00000000000000001"
"25: 0000000000000000000000001"
"33: 000000000000000000000000000000001"
Formatted binary output of  "0x1e"
" 1: 11110"
" 9: 000011110"
"17: 00000000000011110"
"25: 0000000000000000000011110"
"33: 000000000000000000000000000011110"
Formatted binary output of  "0x11"
" 1: 10001"
" 9: 000010001"
"17: 00000000000010001"
"25: 0000000000000000000010001"
"33: 000000000000000000000000000010001"
Formatted binary output of  "0x1111"
" 1: 1000100010001"
" 9: 1000100010001"
"17: 00001000100010001"
"25: 0000000000001000100010001"
"33: 000000000000000000001000100010001"
Formatted binary output of  "0x111e"
" 1: 1000100011110"
" 9: 1000100011110"
"17: 00001000100011110"
"25: 0000000000001000100011110"
"33: 000000000000000000001000100011110"
Formatted binary output of  "0x111f"
" 1: 1000100011111"
" 9: 1000100011111"
"17: 00001000100011111"
"25: 0000000000001000100011111"
"33: 000000000000000000001000100011111"

注:

冒号前的数字 (:) 提供了使用的字段宽度。

示例输出显示,如果结果数字无法正确显示,并且提供的位数作为字段宽度,则输出可能会变长。

这与文档中描述的完全一样。 (由我强调):

fieldWidth specifies the minimum amount of space that a is padded to