(portion of...) QByteArray to UInt
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.
Re: (portion of...) QByteArray to UInt
Code:
uint val;
memcpy(&val, ba.constData(), 2);
or
Code:
uint val;
val = (ba[1] << 8) | ba[0]; // order reversed, as you wanted it
Re: (portion of...) QByteArray to UInt
how about this:
- extracting the chars seperately with at(int)
- anding their appropriate shifted versions
- adding the two results
Code:
char c1 = myByteArray.at(0);
char c2 = myByteArray.at(1);
quint16 myuint = 0;
myuint += (c1<<8)&0xff;
myunint += byte2&0xff;
Re: (portion of...) QByteArray to UInt
Excellent! Again in your debt wysota!
Thank you very much!