473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Big Endian and Little Endian

Hello all,

If I have a C-like data structure such that

struct Data {
int a; //16-bit value
char[3]; //3 ASCII characters
int b; //32-bit value
int c; //24-bit value
}

then assuming I were to store this on a 32 bit wide byte addressable
memory,
then, say, if a= 0A 0B, b=43 44 45, c= 80 00 00 44 and d = 123 (all in
hex), then would I be correct in saying that in a big endian
architecture it
would be stored like the following:

Address 0: 0A 0B 43 44
Address 1: 45 80 00 00
Address 2: 44 00 01 23

and in a little endian:

Address 0: 0B 0A 43 44
Address 1: 45 44 00 00
Address 2: 80 23 01 00

??

I assume this is correct but would appreciate a check nonetheless.

What I wanted to know was if I were to copy
byte by byte (in the more probable scenario you have described) from
the
little endian architecture to the big endian architecture would I end
up
with the same ordering as in the big endian and therefore need to swap
bytes? In other words, if my little endian architecture stores data in
the
following form:

Address 0: 0B 0A 43 44
Address 1: 45 44 00 00
Address 2: 80 23 01 00

and I were to copy this byte by byte to a big endian architecture is
this
what I would end up with:

Address 0: 0B 0A 43 44
Address 1: 45 44 00 00
Address 2: 80 23 01 00

??

or would it look completely different?

What if instead of copying byte by byte I were to copy word by word
(i.e. an
entire address at the time) would I still end up with the same result?

Thanks in anticipation

Jul 7 '06 #1
2 6517
bhatia wrote:
If I have a C-like data structure such that

struct Data {
int a; //16-bit value
char[3]; //3 ASCII characters
int b; //32-bit value
int c; //24-bit value
}
I don't know any C-like language that assigns different sizes to int
according to comments. Asking for concrete answers about pseudocode has
little sense.
hex), then would I be correct in saying that in a big endian
architecture it would be stored like the following:
No. The compiler can add padding bytes to align fields to multiples of 2, or
four, or any other thing the designers of the compiler decide is good for
that machine.

If you copy byte by byte the result is a copy byte by byte, no rocket
science involved. But the tool used to do the copy can have his own
peculiarities.

--
Salu2
Jul 7 '06 #2

"bhatia" <am**************@gmail.comwrote:
Hello all,

If I have a C-like data structure such that

struct Data {
int a; //16-bit value
char[3]; //3 ASCII characters
int b; //32-bit value
int c; //24-bit value
}

then assuming I were to store this on a 32 bit wide byte addressable
memory,
then, say, if a= 0A 0B, b=43 44 45, c= 80 00 00 44 and d = 123 (all in
hex), then would I be correct in saying that in a big endian
architecture it
would be stored like the following:

Address 0: 0A 0B 43 44
Address 1: 45 80 00 00
Address 2: 44 00 01 23

and in a little endian:

Address 0: 0B 0A 43 44
Address 1: 45 44 00 00
Address 2: 80 23 01 00
Not quite. Firstly, address 0 is illegal. Real addresses will always
be non-zero. Secondly, I'm pretty sure most (all?) computers address
data by byte, not by word.

But those things aside, you have roughly the right idea. It would
look more like this, though:

int Var = 0x0A0B4344; // stored at RAM address 0x1B5A3A40, let's say.
Little-Endian representation:
0x1B5A3A40 44
0x1B5A3a41 43
0x1B5A3a42 0B
0x1B5A3a43 0A

Big-Endian representation:
0x1B5A3a40 0A
0x1B5A3a41 0B
0x1B5A3a42 43
0x1B5A3a43 44
If I were to copy byte by byte (in the more probable scenario
you have described) from the little endian architecture to the
big endian architecture would I end up with the same ordering
as in the big endian and therefore need to swap bytes?
Yes. Which is why you wouldn't want to do it byte-by-byte.
Copy the data value by value instead, and you'll be fine.
For instance, say you have an int, 87456, store in a little-endian
RAM or hard-disk. Transfer the whole number to a big-endian system
and it will be stored differently there, but will remain 87456.

If you must transfer data byte-by-byte between systems of different
endian-ness, then you will have to handle the re-ordering of the
bytes in your software. In the case of big-endian to little-endian
(or vice-versa), a simple reversal is called for. One trick I can
think of uses typecasting:

// on a big-endian system:
unsigned char InputArray[4];
unsigned char Reorder[4];
// Load bytes from little-endian system into InputArray.
// Then reorder the bytes:
Reorder[0] = InputArray[3];
Reorder[1] = InputArray[2];
Reorder[2] = InputArray[1];
Reorder[3] = InputArray[0];
// Typecast array of unsigned char back to int:
int MyInteger = *reinterpret_cast<int*>(&Reorder[0]);
// Reuse InputArray and Reorder to reconstitue further 32bit values.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 7 '06 #3

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

Similar topics

2
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
by: Joe C | last post by:
I have some code that performs bitwise operations on files. I'm trying to make the code portable on different endian systems. This is not work/school related...just trying to learn/understand. ...
8
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 }
14
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding...
33
by: raghu | last post by:
Is it possible to know whether a system is little endian or big endian by writing a C program? If so, can anyone please give me the idea to approach... Thanks a ton. Regards, Raghu
8
by: ma740988 | last post by:
Data stored on a storage device is byte swapped. The data is big endian and my PC is little. At issue: There's a composite type ( a header ) at the front of the files that I'm trying to read in....
6
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
by: guthena | last post by:
Write a small C program to determine whether a machine's type is little-endian or big-endian.
23
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.