big to little endian

G

glueless

I have to read files from UNIX systems on my PC. The problem
is that these binary files are in big endian and I need to convert
them. I saw that there are functions ntohl for my visual C++ (4.0),
but I don't know in what librarieses these are.
I want to kep the code as simple as possible, since I also want to
compile it on UNIX systems. Does anyone know of existing functions
that do the conversion as I am a bit lazy to program thi myself?
 
M

Malcolm

glueless said:
I have to read files from UNIX systems on my PC. The problem
is that these binary files are in big endian and I need to convert
them.
The functions fget32be() and fget32le() should be in the armoury of every C
programmer.
Unfortunately you have to write them yourself, but this shouldn't be too
difficult. To be maximally portable you have to sign extend if longs are
over 32 bits, this is something you might want to consider.
 
K

Kenneth Brody

glueless said:
I have to read files from UNIX systems on my PC. The problem
is that these binary files are in big endian and I need to convert
them. I saw that there are functions ntohl for my visual C++ (4.0),
but I don't know in what librarieses these are.
I want to kep the code as simple as possible, since I also want to
compile it on UNIX systems. Does anyone know of existing functions
that do the conversion as I am a bit lazy to program thi myself?

The documentation that came with your C compiler should tell you what
library these functions are in.

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
 
K

Keith Thompson

I have to read files from UNIX systems on my PC. The problem
is that these binary files are in big endian and I need to convert
them. I saw that there are functions ntohl for my visual C++ (4.0),
but I don't know in what librarieses these are.
I want to kep the code as simple as possible, since I also want to
compile it on UNIX systems. Does anyone know of existing functions
that do the conversion as I am a bit lazy to program thi myself?

It depends on the file format. If the files are just, say, a whole
bunch of 32-bit integers catenated together, it shouldn't be too hard.
If, on the other hand, the files contain a mixture of date types
(e.g., the content is effectively an array of structures, where some
of the members are character strings, others are integer, etc.), it's
going to be a bit more difficult. If they contain variable-sized
records, it's going to be even more difficult.

Your best bet might be write a program that reads in the binary file
and generates an unambiguous textual representation, and another that
reads in the textual representation and generates a binary file.
 
E

E. Robert Tisdale

glueless said:
I have to read files from UNIX systems on my PC. The problem is that
these binary files are in big endian and I need to convert them.
I saw that there are functions ntohl for my visual C++ (4.0),
but I don't know in what libraries these are.
I want to kep the code as simple as possible, since I also want to
compile it on UNIX systems. Does anyone know of existing functions
that do the conversion as I am a bit lazy to program thi myself?

The byteorder has nothing to do with UNIX.
It is determined (mostly) by your host machine architecture
Intel x86 computers use little endian and
Motorola/IBM PowerPC computers use big endian
(although they can also speak little endian).
Network byteorder is big endian.

Look for a UNIX man page like this:

BYTEORDER(3) Linux Programmer’s Manual BYTEORDER(3)

NAME
htonl, htons, ntohl, ntohs - convert values
between host and network byte order

SYNOPSIS
#include <netinet/in.h>

uint32_t htonl(uint32_t hostlong);

uint16_t htons(uint16_t hostshort);

uint32_t ntohl(uint32_t netlong);

uint16_t ntohs(uint16_t netshort);
.
.
.

CONFORMING TO
BSD 4.3

SEE ALSO
gethostbyname(3), getservent(3)

BSD 1993-04-15 BYTEORDER(3)


You will need to identify every target platform for your application
so that you can determine whether or not they support these conversions.
You may need to ask more detailed questions in the corresponding
newsgroups.
 
N

Nitin Bhardwaj

I have to read files from UNIX systems on my PC. The problem
is that these binary files are in big endian and I need to convert
them. I saw that there are functions ntohl for my visual C++ (4.0),
but I don't know in what librarieses these are.
I want to kep the code as simple as possible, since I also want to
compile it on UNIX systems. Does anyone know of existing functions
that do the conversion as I am a bit lazy to program thi myself?

htonl() is declared in <winsock2.h>
and u'll have to link with WS2_32.LIB

hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
IRMNikole

Latest Threads

Top