PDA

View Full Version : converting QByteArray to unsigned short



sattu
25th September 2010, 06:27
Hi everyone!
This is sattu again. Can anyone tell me how to convert Unsigned Short to QByteArray?
I don't have any problem in conversion from QByteArray to Unsigned Short, but i also want the reverse conversion which i'm unable to do.:confused:
Can any one please help me?




With Regards,

wysota
25th September 2010, 08:15
What exactly do you mean by "conversion"? Do you want to copy the internal representation of an unsigned short to a byte array or do you want to set the value of the unsigned short in the array or something else?

sattu
27th September 2010, 15:41
What exactly do you mean by "conversion"? Do you want to copy the internal representation of an unsigned short to a byte array or do you want to set the value of the unsigned short in the array or something else?
By this i mean that if i have got an ushort variable, say crc, where crc=11500, then i should be able to copy that to a qbytearray, like:
ushort crc = 11500;
QByteArray byte;
byte += crc;

I did it this way. The hexadecimal equivalent of 11500 is 2CEC. So, when i am adding crc to byte, byte is taking only EC and ommiting the first two 2C. Well that is obvoius because qbytearray stores byte by byte, and at a time it can take only FF(255). So thats why it's taking only EC not 2C.
So, is there any way of doing this?:confused:

Timoteo
27th September 2010, 18:19
sattu,

Why not use QVector<quint16>? What is it you are trying to accomplish?

wysota
27th September 2010, 18:27
QByteArray ba((const char*)&crc, sizeof(ushort));

Just beware it is extremly unportable.

sattu
28th September 2010, 08:17
QByteArray ba((const char*)&crc, sizeof(ushort));

Just beware it is extremly unportable.

Hi Wysota!
Thanks for replying, i tried it the way you have posted. But the same problem is coming. it is taking only EC and leaving the rest. Can you suggest any other way of doing this?

sattu
28th September 2010, 08:27
sattu,

Why not use QVector<quint16>? What is it you are trying to accomplish?

hi Timotoe,
Actually i'm passing some data from server to client. And data passing is possible only through qbytearray or char* way. But the crc of data that i'm calculating is of unsigned short type. But ultimately i have to pass it along with data. That's why i asked this question. So can you suggest me any way?:confused:

tbscope
28th September 2010, 08:37
Dudes... really...


QString::number(ushortNumber, 16)

:-)

wysota
28th September 2010, 09:03
This will make it a string ("2CEC") and not a value ("\x2C\xEC"). I don't know if that's expected but if so then the question was not asked very precisely.

sattu
28th September 2010, 09:10
Dudes... really...


QString::number(ushortNumber, 16)

:-)


Thanks a lot tbscope for this post.
It's working fine. But well, one more question. here i have taken crc=11500(2cec in hex). Here 2C is one byte and EC is another byte. But when i'm storing it in a QByteArray using the code you gave, it is taking 4 bytes instead of 2 bytes. I mean to say it is storing 2,C,E,C in 4 separate boxes(bytes). Now, it is working fine. But is there any way to put this data inside 2bytes of array rather then 4 bytes(which it is normally taking). Sorry to disturb you, but i would be much thankful if you could help me.:confused:


With regards,

sattu
28th September 2010, 09:13
This will make it a string ("2CEC") and not a value ("\x2C\xEC"). I don't know if that's expected but if so then the question was not asked very precisely.

Hi wysota!
You are right, actually i wanted value, not string. But that's not a problem, in the code that tbscope gave, i used QByteArray instead of QString and it is working fine. But the only issue is of memory.

wysota
28th September 2010, 09:52
But that's not a problem, in the code that tbscope gave, i used QByteArray instead of QString and it is working fine. But the only issue is of memory.
You are wrong. The code stores it as a string and not as a value, that's why it's taking four bytes ('2', 'C', 'E', 'C') and not two. The method I gave you should be working fine, maybe you just check it wrong. The code I gave you works just fine.


#include <QtDebug>
#include <QByteArray>

int main(){
unsigned short val = 0x2CEC;
QByteArray ba((const char*)&val, sizeof(val));
qDebug() << "size:" << ba.size();
qDebug() << "hexdump:" << ba.toHex();
return 0;
}
yields:

$ ./ba
size: 2
hexdump: "ec2c"


Note the byte order is reversed (my machine is little endian), hence the unportability I mentioned!

tbscope
28th September 2010, 10:01
it is taking 4 bytes instead of 2 bytes.

I missunderstood. Just ignore my post.

sattu
28th September 2010, 10:52
You are wrong. The code stores it as a string and not as a value, that's why

#include <QtDebug>
#include <QByteArray>
int main(){
unsigned short val = 0x2CEC;
QByteArray ba((const char*)&val, sizeof(val));
qDebug() << "size:" << ba.size();
qDebug() << "hexdump:" << ba.toHex();
return 0;
}
!

Oh yes, you are right. Actually i had checked it wrongly in the locals and watchers section. It is working as you have told. Well exactly speaking even the reverse order is not a problem. we have to send it from the server side in a qbytearray, that's it. But now the problem is how to convert it back to ushort(in the client side)? I have tried it with .toUShort, but it's not working, as the crc is in hexadecimal form. Let me see, if it's possible, otherwise i will have to trouble you again.

many many thanks for helping me,

With regards,;)

tbscope
28th September 2010, 11:06
ushort theNewShort = 0;
memcpy(&theNewShort, ba.data(), sizeof(ushort));

Conditions: the bytearray contains only those two bytes.

sattu
28th September 2010, 12:27
ushort theNewShort = 0;
memcpy(&theNewShort, ba.data(), sizeof(ushort));

Conditions: the bytearray contains only those two bytes.

Thanks tbscope, it's working great. i'm able to do both type of conversion.
but if i use this code to put an ushort into a qbytearray: QByteArray::number(crc, 16); as you had suggested earlier, then how to retrieve back the ushort from the qbytearray again?:confused:

wysota
28th September 2010, 13:51
In this situation you can use QByteArray::toUShort() with 16 as the base.