Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Why does a byte array <-> integer type cast "reverse" order of bytes?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 formalproof   Members   


Posted 10 July 2012 - 04:49 PM

I'm working on a project where I need to convert the bit pattern in an integer type to an array of chars (bytes), and vice versa. I'm wondering why this conversion always seems to "reverse" the order of the bytes. For example, when converting val = 0xAABBCCDD to an array of chars, the first element of the array will be 0xDD, the second 0xCC, etc., although I'm using a little endian system.

For me, the logical order would be 0xAA, 0xBB, 0xCC, 0xDD, because that is the order in which the bytes of val are stored in memory, isn't it?

Here is the code:
#include <iostream>
#include <vector>
using namespace std;
int main()
  int one = 0x1;
  cout << "The system is " << (((one << 1) == 0)? "big endian" : "little endian") << endl;
  // This will output "little endian" on my system
  vector<char> vec;
  unsigned long* ul = reinterpret_cast<unsigned long*>(&vec[0]);
  cout << hex << *ul << endl; // Why does this output 0xDDCCBBAA?

  unsigned long n = 0xAABBCCDD;
  vector<char> w;
  w.assign( reinterpret_cast<char*>(&n), reinterpret_cast<char*>(&n) + sizeof(unsigned long) );
  for(int i = 0; i < w.size(); i++) {
	cout << hex << (int)(unsigned char)w[i]; // this cast is because we want to print the bit pattern of w[i], not the character
  cout << endl; // This will output in the "reverse" order as well: 0xDDCCBBAA0000

So I'm asking whether someone can explain to me why the order of bytes after conversion is 0xDDCCBBAA and not 0xAABBCCDD?

Btw, if you know a better way to convert between an integer type and a vector of chars, or notice any other stupidities in my code, please let me know. :)

#2 ApochPiQ   Moderators   


Posted 10 July 2012 - 04:52 PM

The technical term for this is "endianness".
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#3 formalproof   Members   


Posted 10 July 2012 - 05:08 PM

The technical term for this is "endianness".

Ok, seems I made an embarassing mistake in understanding what little endian means Posted Image

So in a little endian system, val = 0xAABBCCDD is stored in memory as:
0 1 2 4

That would explain why vec[0] == 0xDD, etc. Thanks!

Edited by formalproof, 10 July 2012 - 05:08 PM.

#4 Cornstalks   Members   


Posted 10 July 2012 - 05:17 PM

Just a note: your test for endianness is wrong. (one << 1) will always equal 2, regardless of little endian or big endian. There are a couple of reasons for this. First, a single byte has no endianness (endianness has nothing to do with how the bits are stored in the bytes; it is all about how the bytes are arranged to make bigger data types). It looks like in that test you're expecting the bits to be big/little endian, which is the wrong way to think about it.

Second, operator << is defined such that 1 << 1 is always 2, regardless of endianness (and 1 << 8 is always 256, and 1 << 9 is always 512, etc). It doesn't care about endianness. The compiler is responsible for making sure this holds true, whatever the endianness is.

You don't want to check what the value of something is in order to find out if it's big or little endian. You want to check how the bytes are ordered to find out its endianness.

Endianness is known at compile time (the compiler has to know in order to create the right machine code), so normally people will just check for the appropriate compiler macro flag to see if it's big or little endian. I know of some ways to find out if a computer is big or little endian at run time with code, but these ways that I know make certain assumptions and aren't truly cross platform checks anyway (I don't know of any truly cross platform way to check for endianness).

Edited by Cornstalks, 10 July 2012 - 05:20 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#5 rnlf   Members   


Posted 10 July 2012 - 11:46 PM

I wouldn't even check compiler macros, just use functions like htobe16, le64toh etc. I don't know, if they are available in Microsoft compilers though. At least for up to 32 bits, ntohs, ntohl, htons and htonl should be supported everywhere.

#6 larspensjo   Members   


Posted 13 July 2012 - 07:14 AM

You should never need to check endianess for a compiler or CPU architecture (unless you are in the business of making compilers). When you see someone doing that, they have usually misunderstood something.

You do care about byte order in protocol and binary files, but that should be independent of the current architecture. Please see The byte order fallacy for a more detailed explanation.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.