H
hantheman
Hi all,
I have to convert an integer into a portable byte stream (to disk/network)
Relevant platforms have 32 bit integers, but different endian (big, little, middle).
Is the following a portable solution?
---
union IntegerBuffer { int intval; unsigned char charval[4]; };
IntegerBuffer intbuff;
intbuff.intval = 256;
int index = 0;
for (index = 0; index < sizeof(long int); index++)
{
printf("0x%02x\n", intbuff.charval[index]);
}
I have to convert an integer into a portable byte stream (to disk/network)
Relevant platforms have 32 bit integers, but different endian (big, little, middle).
Is the following a portable solution?
---
union IntegerBuffer { int intval; unsigned char charval[4]; };
IntegerBuffer intbuff;
intbuff.intval = 256;
int index = 0;
for (index = 0; index < sizeof(long int); index++)
{
printf("0x%02x\n", intbuff.charval[index]);
}