PDA

View Full Version : Qbytearray easy question



arturs
16th May 2015, 19:55
Hi

I have the following problem:


QByteArray test;
test[0]= 0xAB;
test[1]= 0xCD;
test[2]= 0xEF;
test[3]= 0x01;
test[4]= 0x02;
test[5]= 0x03;
test[6]= 0x04;

qDebug () << test.at(0);

test.at(0) gives me QChar but I need to display Hex Value

for at(0) is diplayed « instead of 0xAB

How to display hex value ?

Regards
Artur

ChrisW67
16th May 2015, 20:29
QByteArray::toHex() to convert the whole array.
QByteArray::number(value, 16) to do a single byte (Or QString::number(), QString::arg() etc)

arturs
16th May 2015, 20:47
Thank you for help :)

It works.

qDebug() << QByteArray::number(test.at(0),16);

Zlatomir
16th May 2015, 20:49
QDebug (http://doc.qt.io/qt-5/qdebug.html#details) supports QTextStream (http://doc.qt.io/qt-5/qtextstream.html#qtextstream)'s manipulators, so use the hex manipulator and maybe you need to cast the char to an int:

qDebug() << hex << (int)test.at(0);

arturs
16th May 2015, 20:56
something is still wrong ( I make a mistake)

In my case


QByteArray test;
test[0]= 0xAB;
test[1]= 0xCD;
test[2]= 0xEF;
test[3]= 0x01;
test[4]= 0x02;
test[5]= 0x03;
test[6]= 0x04;

for (int m=0;m<7;m++) qDebug() << QByteArray::number(test.at(m),16);


The result is :

"ffffffab"
"ffffffcd"
"ffffffef"
"1"
"2"
"3"
"4"

I do not know why. It would be great to display "01" instead of "1".

Zlatomir
16th May 2015, 21:33
Note that QByteArray is using a signed char* buffer, so you get a truncation for 0xAB and so on (max value is 127, hex 0x7F).

As for your second question, use QString and arg:


for (int m=0;m<7;m++)
qDebug() << hex << QString("%0").arg(test.at(m), 2, 16, QChar('0'));

arturs
17th May 2015, 07:20
How to convert signed byte to unsigned? I receive information from serial port and it is unsigned information.

Regards
Artur

ChrisW67
17th May 2015, 10:34
The each signed char (a byte) in the array can hold a signed decimal value between -128 and 127. The hex representation of the bits in the byte has no sign, so:


QByteArray b = "\x7f\xab\xff";
qDebug() << b.toHex();

Prints "7fabff" as you expect given what we put in the array


// first the signed interpretation of each byte
foreach (char sc, b) {
qDebug() << QByteArray::number(sc) << QByteArray::number(sc, 16);
}



"127" "7f"
"-85" "ffffffffffffffab"
"-1" "ffffffffffffffff"

The extended hex of the negative numbers is the result of sign extension (http://en.wikipedia.org/wiki/Sign_extension) to a 64-bit int as argument to number().


// and the unsigned version
foreach (char sc, b) {
unsigned char uc = static_cast<unsigned char>(sc);
qDebug() << QByteArray::number(uc) << QByteArray::number(uc, 16);
}



"127" "7f"
"171" "ab"
"255" "ff"

You see the same bit pattern being interpreted in two different ways through the static_cast.

Radek
17th May 2015, 16:23
QByteArray::at() or QByteArray[] is a char - not a QChar. The QChar is a result of a conversion made by qDebug(). If you know the size of the QByteArray, then


const unsigned char *pdata = reinterpret_cast<const unsigned char *>(thearray.constData());

gives you a pointer to binary data in the QByteArray. The casting is necessary to get rid of signed values (see above, where 0xA0 was output as 0xFFA0, etc.) Now, you can, for example:


qDebug << pdata[i];

or convert to int by simple assigning:


int val = pdata[i]; // no sign extension

arturs
18th May 2015, 18:25
Could you write me proper code for my code



QByteArray test ;
test[0] = 0xFF;
unsigned char ch = 0;

ch = 0xff;
if (test.at(0)==ch) qDebug () << "OK";

qDebug () << ch;

qDebug () << test.at(0);

because test[0] is signed and ch is unsigned the condition is false. I will try to use your method but without success.

Radek
18th May 2015, 20:23
Try


if( (unsigned char)(test.at(0)) == ch ) qDebug() << "OK";

arturs
18th May 2015, 20:29
It works perfectly :D :D :D :

Thank you for help :)

arturs
19th May 2015, 20:59
I have additional qestion :)


QByteArray test;
test[0]= 0xAB;
test[1]= 0xCD;
test[2]= 0xEF;
test[3]= 0x01;
test[4]= 0x02;
test[5]= 0x03;
test[6]= 0x04;

I need to take only the following bytes
test[1]= 0xCD;
test[2]= 0xEF;
test[4]= 0x02;
test[5]= 0x03;

and show it as a single number

0xCDEF0203 = 3454992899

I try to use the code but it does not work



usnigned long num;
num |= (unsigned char)(test[1]) << 8;
num |= (unsigned char)(test[2]) << 8;
num |= (unsigned char)(test[4]) << 8;
num |= (unsigned char)(test[5]) << 8 ;

Why it does not work ? Maybe is it easier method to do it ?

Regards
Artur

ars
19th May 2015, 21:59
First, in your code the variable num is not initialized before you apply operator |= to it. Second, you shift each of the test[] values by 8 bit and combine the shifted value into num. What you need to do is: initialize num to test[1]. Before adding test[2], shift num by 8 bit and then add test[2]. Same procedure for test[4] and test[5]. It may look like


unsigned long num = (unsigned char)(test[1]) ;
num = num << 8; num |= (unsigned char)(test[2]) ;
num = num << 8; num |= (unsigned char)(test[4]) ;
num = num << 8; num |= (unsigned char)(test[5]) ;

arturs
20th May 2015, 17:31
Yes. It works.

I have last question.

How to convert my QbyteArray test to QString ?

Regards
Artur

ars
20th May 2015, 19:10
See the constructors for QString (http://doc.qt.io/qt-4.8/qstring.html). You can do


QString s(test);

to initalize QString s from QByteArray test. From the documentation:


QString::QString(const QByteArray & ba)

Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromAscii(). Stops copying at the first 0 character, otherwise copies the entire byte array.

You can disable this constructor by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

Radek
20th May 2015, 19:14
Please check the Qt classes reference before posting on the forum.

The wanted QString is produced by "fromXXX" static members of QString. If the QByteArray contains UTF8 data (as a byte array) then


QString test2str = QString::fromUtf8(test);

There are more "fromXXX" functions defined in QString.