Store a signed long int (32bit) as 4 octets?
- by Doori Bar
I managed to get a unsigned long int octets-representation (BE) by reading IPv4 methods, and I managed to read about how signed integers are using the MSB as the sign indicator, which makes 00 00 00 00 to be 0, while 7F FF FF FF is 2147483647.
But I can't manage how to do the same for signed long integers?
#include <stdio.h>
#include <string.h>
int main (void)
{
unsigned long int intu32;
unsigned char octets[4];
intu32 = 255;
octets[3] = (intu32) & 255;
octets[2] = (intu32 >> 8) & 255;
octets[1] = (intu32 >> 16) & 255;
octets[0] = (intu32 >> 24) & 255;
printf("(%d)(%d)(%d)(%d)\n", octets[0], octets[1], octets[2], octets[3]);
intu32 = (octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3];
printf("intu32:%lu\n", intu32);
return 0;
}
Thanks in advance,
Doori bar