473,765 Members | 2,121 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 6338
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
29013
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 under linux debian 3.0 woody Thank you for your help.
3
5192
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 or compiler? 2. In GNU gcc, { int a = {0, 1, 2, 3, 4}; bool b; float c; for (int i = 0; i < 5; i++) {
8
27463
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
6551
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
2627
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 numbers (true) or little endian (false). I tried something like the following, but never did find out if it was correct. There are two assumptions here: First, big endian direction from small to large is ordered like a string. Second, endian is...
6
3177
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 without compression). It has it's words and dwords stored in little- endian, so I do a conversion to big-endian when reading full words or dwords. I have done this because my system is big-endian. But now... what if one compiles the library in a...
23
14184
by: guthena | last post by:
Write a small C program to determine whether a machine's type is little-endian or big-endian.
2
4056
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
7049
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
10156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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 we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.