Jump to content
  • Advertisement
Sign in to follow this  
Aiursrage2k

Big to little

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

//This seems to work It converts a big inedian 32 bit int to Little inedian 32 bit int... but if someone can make sure that it works, that would be good. void BI2LI(int &data) { data = (((data>>28)&0xF)<<4) + ((data>>24)&0xF) + (((data>>20)&0xF)<<12) + (((data>>16)&0xF)<<8) + (((data>>4)&0xF)<<28) + (((data)&0xF)<<24) + (((data>>8)&0xF)<<16) + (((data>>12)&0xF)<<20); }

Share this post


Link to post
Share on other sites
Advertisement
Shouldn't it be something like this:


data = ((data & 0xff000000) >> 24) | // Leftmost -> rightmost
((data & 0xff0000) >> 8) | // Middle-left -> middle-right
((data & 0xff00) << 8) | // Middle-right -> middle-left
((data & 0xff) << 24); // Rightmost -> leftmost


Can't varify that at the moment tho'.

Share this post


Link to post
Share on other sites
I'd rather just not worry about what endian-ness I'm on, and use the host-to-network conversion functions. (network byte order is big endian)

Host to Network Short: htons()
Host to Network Long: htonl()
Network to Host Short: ntohs()
Network to Host Long: ntohl()

No further work needed, it's portable, and should be optimized accoring to your vendor's tastes.

Share this post


Link to post
Share on other sites
And leaves you unprepared when you're given input from a file format defined to be little endian when you're on a big endian machine. ex: a UTF-16LE encoded file.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!