Is this portable?

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]);
}
 
A

Andrey Tarasevich

hantheman said:
...
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]);
}

No.

Firstly, size of an object of type 'int' is not guaranteed to be exactly 4.

Secondly, even if it is 4, the byte order might be different from one
platform to another (big-endian vs. little-endian, for example).

Thirdly, since you are building byte values from _all_ bits of an 'int'
object (not only those that participate in value representation) you
might even get different (inconsistent) values on single platform.

Fourthly, for reasons I don't understand you are cycling value of
'index' from 0 to 'sizeof(long int)'. What is this 'long int' doing
here? Size of type 'long int' might be different from size of type 'int'.
Better ways to do it?

One way to do it is to convert the value to unsigned type and
"calculate" the bytes using arithmetical operations

unsigned value = 12345;
unsigned char byte0 = value % UCHAR_MAX;
unsigned char byte1 = (value >> CHAR_BITS) % UCHAR_MAX;
unsigned char byte2 = (value >> CHAR_BITS * 2) % UCHAR_MAX;
// etc.
 
P

pandy.song

Two Mathod :
1) Convert to ASCII to transfer, and Parse on the other end.

2) All data converted to NetWork-Byte-Order or same endian.

There is no cheap way.
 
A

Andrey Tarasevich

Andrey said:
...
One way to do it is to convert the value to unsigned type and
"calculate" the bytes using arithmetical operations

unsigned value = 12345;
unsigned char byte0 = value % UCHAR_MAX;
unsigned char byte1 = (value >> CHAR_BITS) % UCHAR_MAX;
unsigned char byte2 = (value >> CHAR_BITS * 2) % UCHAR_MAX;
// etc.
...

Of course, the result in this case will depend on values of 'UCHAR_MAX'
and/or 'CHAR_BITS', which can be different from one platform to another.
The problem is that you very question is platform-dependent since you
are using the term "byte" in it. Characteristics of "byte" are
platform-dependent in C++. If you are specifically talking about 8-bit
bytes, then you have to use value 8 instead of 'CHAR_BITS ' and value
256 instead of 'UCHAR_MAX' in the above code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top