By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,906 Members | 1,389 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,906 IT Pros & Developers. It's quick & easy.

big Endian code reading problem

100+
P: 100
Hi,
I have a code which is reading some value stored in beg endian format Which looks like this
Expand|Select|Wrap|Line Numbers
  1. OC_ULong_t g_GetDWord
  2. ( const OC_Byte_t*    pBuffer,
  3.   OC_ULong_t          Offset,
  4.   OC_ByteOrder_t      ByteOrder )
  5. {
  6.   switch(ByteOrder)
  7.   {
  8.     case OC_ByteOrder_BigEndian_e:
  9.       return((pBuffer[Offset] << 24) | (pBuffer[Offset+1] << 16) |
  10.              (pBuffer[Offset+2] << 8) | pBuffer[Offset+3]);
  11.  
  12.     case OC_ByteOrder_LittleEndian_e:
  13.       return((pBuffer[Offset+3] << 24) | (pBuffer[Offset+2] << 16) |
  14.              (pBuffer[Offset+1] << 8) | pBuffer[Offset]);
  15.  
  16.       }
  17. }
  18.  
  19.  
  20.  

Can anyone explain me how it is working??...
May 31 '07 #1
Share this Question
Share on Google+
4 Replies


gpraghuram
Expert 100+
P: 1,275
HI,
Endianness problem comes into picture if u are communicating between a Intel and sparc processor and it occurs for multi-byte data (like int etc).
Basically the endianness is how the data is stored in memory ..
that is MSB first or the LSB first.
To convert the data from little endian to big endian after reading the data the data is swapped .
consider 4 bytes for integers then the 1st byte with fourt and the second byte with the third byte.

Hope u are getting it.
Other members please corect me if i am wrong.
Raghu
May 31 '07 #2

kky2k
P: 34
consider a 16 bit environment...
If hexadecimal representation of a number is ABCD then in little endian system the value will be stored as,

A B C D

if it is a bigendian system then the same will be stored as,

D C B A

it doesn't mean we r reversing the "bits",rather it is reversing the byte order

let me explain ur coding...

return((pBuffer[Offset] << 24) | (pBuffer[Offset+1] << 16) |
(pBuffer[Offset+2] << 8) | pBuffer[Offset+3]);

replace the above code with my example representation

return((D << 24) | (B << 16) | (c << 8) | (A << 0));

D leftshift 24 bits(3 bytes) so it becomes D000

B leftshift 16 bits(2 bytes) so it becomes 0B00

C leftshift 8 bits(1 byte) so it becomes 00C0

A leftshift 0 bits so it becomes 000A

D000
0C00
00B0
000A
--------
DCBA
--------

so A B C D becomes D C B A so u converted a LSB into MSB

so the conclusion is
in ur first case u r converting a LSB into MSB and in the second case u r doing vice versa....

folks correct me if i am wrong
May 31 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
And I would like to add, endian-ness needs to be taken into account when looking a memory with your debugger. You may need to swap the bytes shown to get the int your eyeball wants to see.
May 31 '07 #4

100+
P: 100
Thanks People....Explanation is really good....
:)
Jun 1 '07 #5

Post your reply

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