Say I have the following:
int32 a = ...; // value of variable irrelevant; can be negative
unsigned char *buf = malloc(4); /* assuming octet bytes, this is just big
enough to hold an int32 */
Is there an efficient and portable algorithm to write the two's complement big-endian representation of a
to the 4-byte buffer buf
in a portable way? That is, regardless of how the machine we're running represents integers internally, how can I efficiently write the two's complement representation of a
to the buffer?
This is a C question so you can rely on the C standard to determine if your answer meets the portability requirement.
Yes, you can certainly do it portably:
At least, I think that's right. I'll make a quick test.