By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Is this portable?

P: n/a
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]);
}
---

That is, will it print the same value on all platforms?

Better ways to do it?
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
hantheman wrote:
...
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]);
}
---

That is, will it print the same value on all platforms?
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #2

P: n/a
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.

"hantheman" <ha*********@hotmail.com> wrote in message
news:58**************************@posting.google.c om...
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]);
}
---

That is, will it print the same value on all platforms?

Better ways to do it?

Jul 22 '05 #3

P: n/a
Andrey Tarasevich wrote:
...
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.