PDA

View Full Version : (portion of...) QByteArray to UInt



pdoria
25th July 2009, 13:57
Hi,

I have this QByteArray whose first 2 bytes are, say 0x11 and 0x08.

I'd like to extract those 2 byte into a quint16.

I've tried myByteArray.left(2).toUInt() with no success..

Since these 2 bytes represent a value in little-endian format I must extract them to a quint16 and then qToBigEndian the value.

Could someone please point me the proper way to do it?

TIA,
Pedro.

wysota
25th July 2009, 15:04
uint val;
QByteArray ba;
memcpy(&val, ba.constData(), 2);

or


uint val;
QByteArray ba;
val = (ba[1] << 8) | ba[0]; // order reversed, as you wanted it

tjm
25th July 2009, 15:13
how about this:

extracting the chars seperately with at(int)
anding their appropriate shifted versions
adding the two results




char c1 = myByteArray.at(0);
char c2 = myByteArray.at(1);

quint16 myuint = 0;
myuint += (c1<<8)&0xff;
myunint += byte2&0xff;

pdoria
25th July 2009, 15:23
Excellent! Again in your debt wysota!
Thank you very much!