jeudi 26 février 2015

Endian safe conversion of two chars to unsigned short


I am working with a function that takes two chars, representing an unsigned 16 bit integer, as arguments. I need to convert these values back to an unsigned integer and I would like the method to be endian-safe. I have come up with the method below but I'd like to know if there is a better way. I'm using Qt so the final value is stored in a quint16.



char msb; // most significant byte
char lsb; // least significant byte
quint16 value = ((msb & 0xff) << 8) | (lsb & 0xff);



Aucun commentaire:

Enregistrer un commentaire