Thread: Big Endian Little Endian Complex- Converting Characters

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Big Endian Little Endian Complex- Converting Characters

    I'm reading in a data file which is in little endian format and Im trying to use subroutines to convert int, float, character, etc. into big endian format. I've succeeded in converting the integers from the data file using this subroutine.

    Code:
    int fixint(int *fpt)
    {
    	unsigned char B1, B2, B3, B4;
    	unsigned int swapper;
    	B4 = *fpt & 0x000000FF;
    	     *fpt >>= 8;	    
    	B3 = *fpt & 0x000000FF;
    	     *fpt >>= 8;
    	B2 = *fpt & 0x000000FF;
    	     *fpt >>= 8;
    	B1 = *fpt & 0x000000FF;
    	     *fpt >>= 8;
    		
    	swapper = 0;
    	swapper += B4;
    	swapper <<= 8;
    	swapper += B3;
    	swapper <<= 8;
    	swapper += B2;
    	swapper <<= 8;
    	swapper += B1;
    	*fpt = swapper;
    }
    Obviously this only works for integers but I really have no idea where to start with characters or floating point numbers. lol Please help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Treat everything as an array of bytes and reverse them?

    floats/doubles you will need something like
    Code:
    unsigned char temp[sizeof(double)];
    memcpy ( temp, sourceData, sizeof temp );
    reverse ( temp, sizeof temp );
    memcpy( &myDouble, temp );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    More of a general question :
    What is the best method to find out whether my machine uses little endian or big endian ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The best method is to write code which doesn't care.
    http://c-faq.com/cpp/ifendian.html

    Also, the world isn't totally divided into 'big' and 'little'.
    There are middle-endian machines.
    http://en.wikipedia.org/wiki/Endianness
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Big Endian & Little Endian
    By swaugh in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 11:25 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Replies: 13
    Last Post: 10-09-2006, 09:08 AM
  4. Big and little endian
    By Cactus_Hugger in forum C Programming
    Replies: 4
    Last Post: 10-12-2005, 07:07 PM
  5. Big endian/Little endian conversion
    By bonkey in forum Windows Programming
    Replies: 5
    Last Post: 09-25-2002, 02:29 PM