Aquarius_Girl
Aquarius_Girl

Reputation: 22906

Storing hex in QBytearray, extracting it and converting it to decimal

int dd = 0xA5;

QByteArray p;
p.push_back (0xA5);
qDebug () << "SOP: " << (int)p[0];

This results in -91 whereas 0xA5 stands for 165 in decimal.

How to store hex in QBytearray, extract it and convert it to decimal?

Upvotes: 1

Views: 2011

Answers (1)

Philipp Ludwig
Philipp Ludwig

Reputation: 4190

-91 is just a representation of a char value. char has a range of --127..127. You are storing the value 165, which is larger than 127.

However, unsigned char has a range of 0..255. So in this case you may read your value as an unsigned char:

qDebug() << "SOP: " << (unsigned char)p[0];

In addition you may use QString to display the corresponding hex value:

QString hex = QString("%1").arg((unsigned char)p[0] , 0, 16);
qDebug() << "Hex: " << hex;

Upvotes: 2

Related Questions