简体   繁体   English

如何将QBytearray BCD转换为十进制QString表示形式?

[英]How To Convert QBytearray BCD to Decimal QString Representation?

Hi I read a Packed BCD from a file that I Want to convert it to it's decimal representation. 嗨,我从文件中读取了压缩的BCD,我想将其转换为十进制表示形式。 the data length is 32 bytes and for example this is what is in the file : 数据长度为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

I want to show the data as it is how can i do it? 我想显示数据,因为我该怎么办?

thanks schef it worked for me. 谢谢谢夫,它为我工作。 I have another question: I the data that I read some of data are numerical data that are in raw hex format for eeample : 我还有另一个问题:我读取的一些数据是数值数据,这些数据是eeample的原始十六进制格式:

22 d8 ce 2d

that must interpreted as: 必须解释为:

584633901

what is the best and quickest way? 最好最快的方法是什么? currntly I do it like this: 我现在这样做:

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

and for 1 and 2 byte integers I do it like this: 对于1和2个字节的整数,我会这样:

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

I started looking into QByteArray whether something appropriate is already available but I couldn't find anything. 我开始调查QByteArray是否已经提供了合适的东西,但找不到任何东西。 Hence, I just wrote a loop. 因此,我只写了一个循环。

testQBCD.cc : 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 : testQBCD.pro

SOURCES = testQBCD.cc

QT += widgets

Compile and test (cygwin64, Window 10 64 bit): 编译和测试(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"

$

I hope I interpreted the term "packed BCD" correctly. 我希望我正确解释了“打包的BCD”一词。 I believe I did (at least according to Wikipedia Binary-coded decimal – Packed BCD ). 我相信我做到了(至少根据Wikipedia 二进制编码的十进制–打包的BCD )。 If support of sign becomes an issue this would mean some additional bit-twiddling. 如果符号支持成为一个问题,这将意味着一些额外的纠结。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM