Re: Qbytearray easy question
QByteArray::toHex() to convert the whole array.
QByteArray::number(value, 16) to do a single byte (Or QString::number(), QString::arg() etc)
Re: Qbytearray easy question
Thank you for help :)
It works.
qDebug() << QByteArray::number(test.at(0),16);
Re: Qbytearray easy question
QDebug supports QTextStream's manipulators, so use the hex manipulator and maybe you need to cast the char to an int:
Code:
qDebug() << hex << (int)test.at(0);
Re: Qbytearray easy question
something is still wrong ( I make a mistake)
In my case
Code:
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".
Re: Qbytearray easy question
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:
Code:
for (int m=0;m<7;m++)
qDebug
() << hex <<
QString("%0").
arg(test.
at(m
),
2,
16,
QChar('0'));
Re: Qbytearray easy question
How to convert signed byte to unsigned? I receive information from serial port and it is unsigned information.
Regards
Artur
Re: Qbytearray easy question
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:
Code:
qDebug() << b.toHex();
Prints "7fabff" as you expect given what we put in the array
Code:
// first the signed interpretation of each byte
foreach (char sc, b) {
}
Code:
"127" "7f"
"-85" "ffffffffffffffab"
"-1" "ffffffffffffffff"
The extended hex of the negative numbers is the result of sign extension to a 64-bit int as argument to number().
Code:
// and the unsigned version
foreach (char sc, b) {
unsigned char uc = static_cast<unsigned char>(sc);
}
Code:
"127" "7f"
"171" "ab"
"255" "ff"
You see the same bit pattern being interpreted in two different ways through the static_cast.
Re: Qbytearray easy question
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
Code:
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:
Code:
qDebug << pdata[i];
or convert to int by simple assigning:
Code:
int val = pdata[i]; // no sign extension
Re: Qbytearray easy question
Could you write me proper code for my code
Code:
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.
Re: Qbytearray easy question
Try
Code:
if( (unsigned char)(test.at(0)) == ch ) qDebug() << "OK";
Re: Qbytearray easy question
It works perfectly :D :D :D :
Thank you for help :)
Re: Qbytearray easy question
I have additional qestion :)
Code:
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
Code:
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
Re: Qbytearray easy question
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
Code:
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]) ;
Re: Qbytearray easy question
Yes. It works.
I have last question.
How to convert my QbyteArray test to QString ?
Regards
Artur
Re: Qbytearray easy question
See the constructors for QString (http://doc.qt.io/qt-4.8/qstring.html). You can do
to initalize QString s from QByteArray test. From the documentation:
Quote:
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.
Re: Qbytearray easy question
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
There are more "fromXXX" functions defined in QString.