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

Home Posts Topics Members FAQ

Flip from little endian to big endian ?

66 New Member
Is there a quick method to flip endian-ness in C? Different platforms I am using, use big and little endian, and I want to flip everything to big.
Jan 6 '09 #1
6 6305
Banfa
9,065 Recognized Expert Moderator Expert
Why?

To expand on my question if you mean you want to use big endian for all data storage no matter what platform is used then that is a mistake. You should let the platform work in it's native endian, anything else will be atrociously in-efficient.

The only things that should have fixed endian are those things that are truely platform independent, that is communication protocols, file formats and similar methods of data transfer.

You can then easily write functions that read and write data out of the file (or protocol etc.) as bytes and combines those bytes into integers etc in a portible manor using the bitwise operators (shifts, ors and ands).
Jan 6 '09 #2
gpraghuram
1,275 Recognized Expert Top Contributor
There are already readymade functions for this like ntoh,hton etc.

Raghu
Jan 6 '09 #3
Banfa
9,065 Recognized Expert Moderator Expert
Like I have just said elsewhere ntoh, hton are part of a third party library and are not available on every platform and therefore are inherently non-portable.

The portable method is to use the bitwise operators.
Jan 6 '09 #4
dissectcode
66 New Member
Why?

Because when we display results from an address, they are reversed, depending on the platform.
Jan 6 '09 #5
Banfa
9,065 Recognized Expert Moderator Expert
A memory address? An IP address? You are not explaining your problem fully.

Do these platforms communicate with each other or are they sealed pieces of software, i.e. do you have problem of sending data between platforms with different endians or have you managed to write non-portable software and then transferred it to a platform that it doesn't work properly on?
Jan 6 '09 #6
donbock
2,426 Recognized Expert Top Contributor
@dissectcode
It sounds like you're describing some sort of debugger: you type in an address and it prints out the contents of that address. If so, then I don't see why you need to worry about endianness -- simply cast the address to the appropriate pointer type and dereference the pointer.

In my experience, the only times when I needed to worry about endianness were:

1. Reading or writing a data file that might be used by many platforms. In this case it was important to specify the endianness of the data file so each platform could properly access it.

2. Reading or writing a message stream that might be used by many platforms. In this case it was important to specify the endianness of the message so each platform could properly access it.

3. PCI accesses. The PCI standard specifies the endianness of the memory-mapped I/O, so the platform has comply with that endianness even if it does't match the native endianness of the processor.

4. Interpreting panic dump printouts. [Now I'm showing my age -- does anybody still have to deal with these?]

The common thread here is that endianness only needs to be explicitly taken into account when information is passed between platforms that might have different native endianness.
Jan 6 '09 #7

Sign in to post your reply or Sign up for a free account.

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 }
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
4027
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
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
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
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6894
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...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
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
3099
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
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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 ...

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.