Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


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   -  Reputation: 165

Like
0Likes
Like

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;
  vec.push_back(0xAA);
  vec.push_back(0xBB);
  vec.push_back(0xCC);
  vec.push_back(0xDD);
  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. :)

Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 14102

Like
2Likes
Like

Posted 10 July 2012 - 04:52 PM

The technical term for this is "endianness".

#3 formalproof   Members   -  Reputation: 165

Like
0Likes
Like

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
DD CC BB AA

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

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


#4 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

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   -  Reputation: 1056

Like
1Likes
Like

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.

my blog (German)


#6 larspensjo   Members   -  Reputation: 1526

Like
1Likes
Like

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.



PARTNERS