| | |
Typecasting on littleendian and bigendian
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
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
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> Is there any standard function to test the endianess of the system?
c++ Syntax (Toggle Plain Text)
inline bool little_endian() { union { long i ; char c[ sizeof(long) ] ; }; i = 1 ; return c[0] == 1 ; }
> 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
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
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:
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
Hi Salem,
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
•
•
•
•
> 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
(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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: using a vector::iterator
- Next Thread: Reading memory from another program
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






