473,499 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

big to little endian

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?
Nov 14 '05 #1
5 10195

"glueless" <ma***********@web.de> wrote in message
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.
Nov 14 '05 #2
glueless wrote:

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." |
+---------+----------------------------------+-----------------------------+

Nov 14 '05 #3
ma***********@web.de (glueless) writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #4
glueless wrote:
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);
Nov 14 '05 #5
ma***********@web.de (glueless) wrote in message news:<48**************************@posting.google. com>...
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
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
28994
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
3
5176
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
8
27435
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
2
6527
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
5
3137
by: manishster | last post by:
I keep getting the following in my output file , regardless of whether I convert endian-ness or not . How do i get "01 02 03 04" .... Mahamannu output : 04 03 02 01 b0 00 00
3
2606
RRick
by: RRick | last post by:
This was a question that showed up in a job interview once. (And to answer your next question: No, I didn't :)) Write a subroutine that returns a bool on whether a system supports big endian...
6
3159
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
23
14150
by: guthena | last post by:
Write a small C program to determine whether a machine's type is little-endian or big-endian.
2
4026
by: Ramesh | last post by:
Hi I have a structure as below on big endian based system typedef struct { unsigned long LedA:5; unsigned long LedB:4; unsigned long LedC:8;
23
7004
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7215
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...
1
6892
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
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.