
Originally Posted by
sattu
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;
qDebug() << "size:" << ba.size();
qDebug() << "hexdump:" << ba.toHex();
return 0;
}
#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;
}
To copy to clipboard, switch view to plain text mode
yields:
$ ./ba
size: 2
hexdump: "ec2c"
$ ./ba
size: 2
hexdump: "ec2c"
To copy to clipboard, switch view to plain text mode
Note the byte order is reversed (my machine is little endian), hence the unportability I mentioned!
Bookmarks