【问题标题】:How To Convert QBytearray BCD to Decimal QString Representation?如何将 QBytearray BCD 转换为十进制 QString 表示?
【发布时间】:2018-12-30 14:39:48
【问题描述】:

您好,我从一个文件中读取了一个压缩 BCD,我想将其转换为十进制表示。 数据长度为 32 字节,例如这是文件中的内容:

95 32 07 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 01 00 13 00

我想按原样显示数据,我该怎么做?

感谢 schef 它对我有用。 我有另一个问题: 我读取的一些数据是用于eeample的原始十六进制格式的数字数据:

22 d8 ce 2d

必须解释为:

584633901

什么是最好和最快的方法? 目前我是这样做的:

QByteArray DTByteArray("\x22 \xd8 \xce \x2d");
QDataStream dstream(DTByteArray);
dstream.setByteOrder(QDataStream::BigEndian);
qint32 number;
dstream>>number;

对于 1 和 2 字节整数,我这样做:

QString::number(ain.toHex(0).toUInt(Q_NULLPTR,16));

【问题讨论】:

  • 一般来说,一个问题一个问题。关于你的第二个问题:这是一种可能的方式,虽然我不确定是否最快。另一种选择是使用位运算符在普通 C++ 中执行此操作。

标签: c++ qt5 qstring qbytearray bcd


【解决方案1】:

我开始查看QByteArray 是否已经有合适的东西可用,但我找不到任何东西。因此,我只写了一个循环。

testQBCD.cc:

#include <QtWidgets>

int main()
{
  QByteArray qBCD(
    "\x95\x32\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x13\x00",
    32);
  QString text; const char *sep = "";
  for (unsigned char byte : qBCD) {
    text += sep;
    if (byte >= 10) text += '0' + (byte >> 4);
    text += '0' + (byte & 0xf);
    sep = " ";
  }
  qDebug() << "text:" << text;
  return 0;
}

testQBCD.pro:

SOURCES = testQBCD.cc

QT += widgets

编译测试(cygwin64,Window 10 64位):

$ qmake-qt5 

$ make
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQBCD.o testQBCD.cc
g++  -o testQBCD.exe testQBCD.o   -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread 

$ ./testQBCD 
text: "95 32 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 13 0"

$

我希望我正确解释了“压缩 BCD”这个术语。我相信我做到了(至少根据维基百科Binary-coded decimal – Packed BCD)。如果对符号的支持成为一个问题,这将意味着一些额外的位旋转。

【讨论】:

  • 嗨,谢谢它对我有用。我还有一个问题:
猜你喜欢
  • 2017-08-31
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多