Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 2342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; // this cast is because we want to print the bit pattern of w, 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. :)

Share this post

Link to post
Share on other sites

The technical term for this is "endianness".

Ok, seems I made an embarassing mistake in understanding what little endian means smile.png

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

Share this post

Link to post
Share on other sites
Just a note: your test for endianness is wrong. [font=courier new,courier,monospace](one << 1)[/font] 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

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!