Typecasting on littleendian and bigendian

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Typecasting on littleendian and bigendian

 
0
  #1
May 14th, 2008
hello,

I have the following problem
I have an unsigned long variable, but it is designed to hold values within 0 .. 100 ( I know an unsigned char would have been enough, but i have to use an unsigned long ).
I have to write that value in a file on a single byte.

if i do fwrite(&unsignedlongvar, 1, 1, file ) it will write the octet on the lowest adress of the variable. If the system is little endian this is ok because that is where the value I am intrested in is, but if the system would be big endian the value I am intrested in would be at the highest adress and the fwrite would write the lowest octet. How do you suggest solving the porting problem? Is there any standard function to test the endianess of the system?

thx in advance
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Typecasting on littleendian and bigendian

 
0
  #2
May 14th, 2008
what about solving little/big endian problem by doing appropriate typecast before writing to file?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Typecasting on littleendian and bigendian

 
0
  #3
May 14th, 2008
> Is there any standard function to test the endianess of the system?
  1. inline bool little_endian()
  2. {
  3. union
  4. {
  5. long i ;
  6. char c[ sizeof(long) ] ;
  7. };
  8. i = 1 ;
  9. return c[0] == 1 ;
  10. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Typecasting on littleendian and bigendian

 
1
  #4
May 14th, 2008
> what about solving little/big endian problem by doing appropriate typecast before writing to file?
Because endian can't be fixed with a simple cast.
You have to write code to rearrange the bytes into the correct order.

> Is there any standard function to test the endianess of the system?
Not only is this non-portable (it assumes far too much about internal representations), it also assumes that there are only two endian systems (there's at least 3).
http://en.wikipedia.org/wiki/Endianness
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Typecasting on littleendian and bigendian

 
0
  #5
May 14th, 2008
If you know your target value will fit in a smaller integer, you really don't need to worry about endianness. Do as tesuji suggests and let the compiler do the appropriate movement:
  1. fputc( unsignedlongvar, file ); // bits 8..31 are lost!

Don't use fwrite() for size-specific I/O. The C and C++ standards require a char to be one byte (eight bits), but there is no requirement on internal representation. So it is possible to find some funky system/compiler that stores them in word values... and if the endianness isn't strictly little-endian, then using fwrite() won't work.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Typecasting on littleendian and bigendian

 
0
  #6
May 14th, 2008
Hi Salem,

Originally Posted by Salem View Post
> what about solving little/big endian problem by doing appropriate typecast before writing to file?
Because endian can't be fixed with a simple cast.
You have to write code to rearrange the bytes into the correct order.

> Is there any standard function to test the endianess of the system?
Not only is this non-portable (it assumes far too much about internal representations), it also assumes that there are only two endian systems (there's at least 3).
http://en.wikipedia.org/wiki/Endianness
This is a completely wrong conclusion (maybe you didn't stick to the very original question)

No matter hardware is based on little-, mid- or big-endian, typecast from longer to shorter representation (and vice versa), for example from long double to float, will be correctly carried out. Apart from general loss of precision due to shorting the mantissa, serious problem may occur if the receiving exponent area is too short, what would lead to overflow. But this problems aren't caused by how the byte sequence is physically ordered in memory. Fairly occasionally one will get confronted with the endian problem, for example if one does TCP/IP programming, where data structures often still stick to big-endian tradition from Digital Equipment era.

Oh yes, no, there is no flag telling you "hello, I am little endian", you have to know it!

btw, if you know what endian your data is coded, swapping the bytes to get another endian coding is a very plain task.

krs,
tesu
Last edited by tesuji; May 14th, 2008 at 7:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Typecasting on littleendian and bigendian

 
0
  #7
May 15th, 2008
> Is there any standard function to test the endianess of the system?
This was an original question - what are you smoking?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC