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
There is no difference. You can always serialize/deserialize signed integers as if they are unsigned, the difference is only in the interpretation of the bits, not in the bits themselves.
Of course, this only holds true if you know that the unsigned and signed integers are of the same size, so that no bits get lost.
Also, you need to be careful (as you are) that no intermediary stage does any unplanned sign-extension or the like, the use of
unsigned charfor individual bytes is a good idea.