473,241 Members | 1,576 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,241 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 6506
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
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.