PDA

View Full Version : store number in QByteArray



sattu
20th January 2011, 13:33
hi everyone,
i want to store a decimal value into a QByteArray.



int num = 43981;//abcd
QByteArray byte((const char*)&num,sizeof(num);//byte[0] =cd,byte[1]=ab



this stores the number.But it in reverse way.ie byte contain "cd ab".
But i want to store in "ab cd" way.

plz help me.

high_flyer
20th January 2011, 13:46
QByteArray byte;
int num = 43981;
for(int i=sizeof(int)-1; i>0; i--){
byte.append((char)(num>>i));
}


Note: didn't test it.

wysota
20th January 2011, 14:27
You can also try:

quint16 num = 43981; // note that "int" is 4 bytes long and not 2 as in your code
QByteArray byte(2, 0);
qToBigEndian(num, byte.data());