簡體   English   中英

如何將QBytearray BCD轉換為十進制QString表示形式?

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

嗨,我從文件中讀取了壓縮的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

我想顯示數據,因為我該怎么辦?

謝謝謝夫,它為我工作。 我還有另一個問題:我讀取的一些數據是數值數據,這些數據是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));

我開始調查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”一詞。 我相信我做到了(至少根據Wikipedia 二進制編碼的十進制–打包的BCD )。 如果符號支持成為一個問題,這將意味着一些額外的糾結。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM