473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Is this portable?

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
3 1333
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: James Harris | last post by:
Hi, Can someone recommend a book that will teach me how to approach C programming so that code is modularised, will compile for different environments (such as variations of Unix and Windows),...
22
by: SeeBelow | last post by:
Is there any way, in C, of interacting with a running program, but still using code that is portable, at least between linux and Windows? By "interacting" it could be something as simple as...
10
by: Jason Curl | last post by:
Dear C group, I'm very interested in writing portable C, but I only have GNU, Sparc and Cygwin to compile on. What I find is the biggest problem to writing portable C is what headers to...
8
by: suresh_C# | last post by:
Dear All, What is difference between Portable Executable (PE) file and a Assembly? Thanks, Mahesh
2
by: Tull Clancey | last post by:
Hi all. I'm nearing completion of a host app that needs to send data to, and receive data back from a portable, an HP device. The application running on the portable will be a .net application....
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
162
by: Richard Heathfield | last post by:
I found something interesting on the Web today, purely by chance. It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm not sure which. ...
409
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are...
10
by: bramnizzle | last post by:
I don't know if this is the right thread or not, but... In my endless pursuit to hold on to my dinosaur laptop (Dell 1100 Inspiron - circa 2004)...I want to keep as much space free for my...
23
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.