Archived

This topic is now archived and is closed to further replies.

big/little-endian problem

This topic is 5110 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

OK, here is the problem. I was trying to write out bytes of some vars when I noticed this:
// This program basicly writes out each byte starting from FIRST

DWORD a = 1;
for (DWORD i = 0; i < sizeof(DWORD); ++i)
{
    std::cout << (DWORD)( *((BYTE*)&a + i )) << " ";
}
std::cout << std::endl;
Output is ''1 0 0 0''. This was strange at first moment because logicaly, output should be ''0 0 0 1''. Then I read about big-endian, little-endian thing and I realized, after I read that network traffic is always big-endian, I should always try to write in latter way. However, I don''t know which proccessors are little and which are big endian. Is this actually a problem if I am only targeting Windows machines? Can you tell me which way is right. Also, is there a way to know on which proccessor my program is running so I can change loading order if neccessary.

Share this post


Link to post
Share on other sites
He means x86/Motorola/Alpha/Sparc...etc.

The endian-ness is determined at compile time, not run time.

The "network order" is always big-endian, and you can use
functions such as "htonl" (Host To Network Long) and "ntohl"
(Network to Host Long) to convert back and forth.

Using these functions, your program will compile and run
correctly, regardless of the endian-ness of the CPU.




Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites