c++ - Change the width of a signed integer to a nonstandard width -
for networking application need signed, 2's complement integer. custom width. specified @ run time. assuming value of integer falls in width.
the problem have parity bit. there way of avoid having manually set parity bit? have integer width of 11 bits, i'll store in array of 2 chars this:
int myintwidth = 11; int32_t myint= 5; unsigned char chararray[2] = memcpy(chararray, &myint, (myintwidth + 7)/8);
it doesn't work that. can't work, because copying 2 bytes start of myint don't know bytes interested in stored. need know in order supposed store bytes. depending on that, use 1 of these 2 codes:
unsigned char chararray [2]; chararray [0] = myint & 0xff; // lowest 8 bits chararray [1] = (myint >> 8) & 0x07; // next 3 bits
or
unsigned char chararray [2]; chararray [1] = myint & 0xff; // lowest 8 bits chararray [0] = (myint >> 8) & 0x07; // next 3 bits
Comments
Post a Comment