1

Hi I read a Packed BCD from a file that I Want to convert it to it's decimal representation. the data length is 32 bytes and for example this is what is in the file :

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 :

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:

QString::number(ain.toHex(0).toUInt(Q_NULLPTR,16));
pumper
  • 141
  • 1
  • 10
  • In general, one question per question. Concerning your second problem: it's a possible way though I'm not sure whether the fastest. Another option would be to do this in plain C++ with bit operators. – Scheff's Cat Jul 23 '18 at 13:47

1 Answers1

0

I started looking into QByteArray whether something appropriate is already available but I couldn't find anything. Hence, I just wrote a loop.

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

Compile and test (cygwin64, Window 10 64 bit):

$ 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. I believe I did (at least according to Wikipedia Binary-coded decimal – Packed BCD). If support of sign becomes an issue this would mean some additional bit-twiddling.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56