Sign in to follow this  
Aiursrage2k

Big to little

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this