![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
Senior Member
Join Date: Aug 2004
Location: USA
Posts: 829
|
Code:
When programming across networks it is often necessary to transfer data between a machine that is little endian and a machine that is big endian. See the Wikipedia entry for more information on the subject. Generally network transmission should be in big endian. This function will prep data for sending across the network and do the reverse for data recieved across the network. This way the endianness of the opposite machine does not matter. First it checks to see if the first byte is the same as the least significant byte. If this is the case then the machine is little endian and the data must be converted.
___________________________________________
Jesse Coyle |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
cool. never noticed this one
![]()
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Aug 2004
Location: USA
Posts: 829
|
I basically never use assembly so I didn't know about that, but the whole point of the function was to be as portable as possible, I figure if I don't know the endianness of the machine I probably wont know the archetecture either. You are right though, if this is used in a performance critical location it is better to sacrifice readability in order to put in multiple code paths in so you can use the assembly of each architecture. The apps I was writing were not proccessor bound on network transmission though, so I didn't have to worry about optomization.
![]()
___________________________________________
Jesse Coyle |
|
|
|
|
|
#5 |
|
Member
Join Date: Sep 2004
Posts: 67
|
You could also simply use htonl and the likes.
|
|
|
|
|
|
#6 |
|
New Member
Join Date: Sep 2004
Location: Switzerland
Posts: 1
|
Code:
I don't think thats a good idea. Think about a value like 0xFFABCDFF. The above code will determine the 'machine' to be little endian, although it doesn't necessary need to be. You basically compare the first byte of the value with the total value modulo 256. This will lead to potentially wrong results if the first and last byte are identical. Or am I missing the obvious?? It's still early morning :) |
|
|
|
|
|
#7 |
|
Member
Join Date: Sep 2004
Posts: 67
|
No, I think that's a bug. You should do the check on some known data, such as:
Code:
Or just use htonl etc... |
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Aug 2004
Location: USA
Posts: 829
|
There is an error in the code but that is not it. I modulus by 256 to zero out the other bytes, but this would not work if the lower byte is zero already. I will fix the code to accomidate this.
___________________________________________
Jesse Coyle |
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Aug 2004
Location: USA
Posts: 829
|
I suppose even better I could use
Code:
___________________________________________
Jesse Coyle |
|
|
|
|
|
#10 | |
|
Member
Join Date: Sep 2004
Posts: 67
|
Quote:
|
|
|
|
|
|
|
#11 |
|
Senior Member
Join Date: Aug 2004
Location: USA
Posts: 829
|
Ok, what is wrong with that?
___________________________________________
Jesse Coyle |
|
|
|
|
|
#12 |
|
Member
Join Date: Sep 2004
Posts: 67
|
It doesn't return 0 for big endian machines.
|
|
|
|
|
|
#13 |
|
Senior Member
Join Date: Aug 2004
Location: USA
Posts: 829
|
Quite true, good catch. Corrected as such. I really wish I didn't have to worry too much about the low level, but it is a good learning experience. Hopefully others can learn from this too.
BTW, I checked up on htonl, this is basically what I attempt to implement. Unfortunatly I do not have endian.h where it is supposed to reside on my mingw compiler. Is this a linux only include?
___________________________________________
Jesse Coyle |
|
|
|
|
|
#14 |
|
Valued Member
Join Date: Aug 2004
Location: Quebec, Canada
Posts: 109
|
Here's what you can do:
Code:
|
|
|
|
|
|
#15 |
|
Valued Member
Join Date: Aug 2004
Location: Quebec, Canada
Posts: 109
|
damn I think I should pay more attention before posting...
sorry for stating the already stated ![]() |
|
|
|
|
|
#16 |
|
Member
Join Date: Sep 2004
Posts: 67
|
You could include one of the WinSock headers.
|
|
|
|
|
|
#17 |
|
Senior Member
Join Date: Aug 2004
Location: USA
Posts: 829
|
The whole idea is for this to work outside of windows x86 systems as well as on those systems.
I was writing an app in C that could be compiled on windows, linux, or solaris.
___________________________________________
Jesse Coyle |
|
|
|
|
|
#18 |
|
Member
Join Date: Sep 2004
Posts: 67
|
htonl etc are available on basically every platform that has a BSD-style socket interface.
|
|
|
|
|
|
#19 |
|
Senior Member
Join Date: Aug 2004
Location: USA
Posts: 829
|
I wanted to try and avoid a page of #if / #includes to find the right headers on any system
___________________________________________
Jesse Coyle |
|
|
|
|
|
#20 |
|
Member
Join Date: Sep 2004
Posts: 67
|
If you would restrict your set of a platforms to the relevant (quasi)standard, and the relevant (quasi)standard would enforce the existance of certain header files, that was possible. <wink>
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|