What I have is this
struct Record
{
unsigned char cat;
unsigned char len[2]={0x00, 0x1b}; // can't put short here because that
// whould change the size of the struct
unsigned char dat[253];
};
Record record;
unsigned short recordlen = *((unsigned short*)record.len);
This result in recordlen=0x1b00 instead of 0x001b
Same with *reinterpret_cast<unsigned short*>(record.len)
Can you explain why ? How should I be doing this ?
Because you cannot assume a specific endianness of your computer architecture.
The natural follow-up question is what do you do about it. Fortunately, you can force a specific byte order by calling one of these functions
htonl,htons,ntohl, orntohs. They work regardless of the computer architecture on which you run them:On the sending end, you convert from host order to network order; on the receiving end, you convert from network order to host order.