Jump to content
  • Advertisement

Archived

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

Toni Petrina

big/little-endian problem

This topic is 5276 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
Advertisement
You'll need a different compiler for each processor platform anyway.

-Nik

[edited by - Nik02 on December 14, 2003 9:10:04 AM]

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
About the network traffic thing, check out the functions:

htons - host to network short
ntohs - network to host short
htonl - host to network long
ntohl - network to host long

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!