473,394 Members | 2,168 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,394 software developers and data experts.

big Endian code reading problem

100 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
4 2607
gpraghuram
1,275 Expert 1GB
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
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
9,208 Expert Mod 8TB
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
rag84dec
100 100+
Thanks People....Explanation is really good....
:)
Jun 1 '07 #5

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

Similar topics

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 }
11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
20
by: Aaron Graham | last post by:
/** * Sample usage: * unsigned long longvar = 0x12345678; * unsigned long be_longvar = endian::host_to_big(longvar); * unsigned short shortvar = 0x1234; * unsigned short le_shortvar =...
6
by: Karl Seguin [MVP] | last post by:
The BinaryWriter/Reader's are little-endian only. Was hoping they were going to be made more flexible in 2.0, but that doesn't appear to be the case. Anyone already done all the work of...
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...
1
by: Andrea Gavana | last post by:
Hello NG, that may sound a silly question, but I didn't find anything really clear about the issue of reading unformatted big endian files with Python. What I was doing till now, was using...
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
11
by: kolmogolov | last post by:
hi, it's not really an endian problem. I think I must be missing something else ... The problem can be reduced to different results of the following two segments of codes: (cut and pasted...
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...
13
by: Guillaume Dargaud | last post by:
Hello all, I tried to write a macro to check if a system is big endian or little endian with no success. Don't get me wrong, I have no problem writing a function or a one liner to do that, but I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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
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...

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.