doublec16
10-13-2003, 05:49 PM
I have a binary file that is in the UNIX Sun big endian format (most significant byte first) and I want to use the fread function in a C program to read the numbers in Linux x86, which uses the little endian byte order (least significant byte first). Is there a simple way to do this in a C program? Let's say I have an integer n which I have read with
fread(&n, 4, 1, infile);
This integer is in the exact opposite byte order so that it's the wrong number. Are there a few lines of code I can use to make it the correct number? I think there should be a simple way to do it but I'm not that good with pointers and such. Perhaps there is an environment variable I can set?
Thanks.
PS I have found info about ntoh and hton which convert between the host protocol and network (big endian) protocol, but I'm not sure how to use them. Are they built in C functions, or are they separate?
fread(&n, 4, 1, infile);
This integer is in the exact opposite byte order so that it's the wrong number. Are there a few lines of code I can use to make it the correct number? I think there should be a simple way to do it but I'm not that good with pointers and such. Perhaps there is an environment variable I can set?
Thanks.
PS I have found info about ntoh and hton which convert between the host protocol and network (big endian) protocol, but I'm not sure how to use them. Are they built in C functions, or are they separate?