#### Archived

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

# 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.

## 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 on other sites
You'll need a different compiler for each processor platform anyway.

-Nik

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

##### Share on other sites
You don''t mean that I need defferent versions for AMD/Intel proccessors or you mean for x86/Motorola/Alpha?

##### 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 on other sites
So you mean, I don''t have anything to worry about? Thanks a lot for your replies.

##### 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

• 9
• 9
• 13
• 41
• 15