Thread: Swapping endiannes

  1. #1
    Registered User Rennor's Avatar
    Join Date
    Aug 2006
    Location
    Finland
    Posts
    45

    Swapping endiannes

    Hello you all familiar with big-ednian and small-endian. With Motorola processors I stumbled on this little fact that values are in different byte orders in Motorola and Intel systems. Woot.

    So, I made small C function to swap 32bit integer values for my Cantata++ test platform while reading real binary file which is copied to memory and act's as embedded RAM/ROM during the test which is not connected to unit at all.

    Here's the function:
    Code:
    #define u32  unsigned __int32 // Definition for Cantata++
    #define u8 unsigned char
    
    u32 swap( u32 val )
    {
    	u32 temp, iLoop;
    	u8 *pDest = (u8 *)&temp;
    	u8 *pSrc = (u8 *)&val;
    
    	for( iLoop = 0; iLoop < sizeof( val ); iLoop++ )
    		*(pDest + iLoop) = *((pSrc+sizeof( val ))-(iLoop+1));
    
    	return temp;
    }
    Now, I believe this will cause alot of debate (which I am actually looking for). I know it is best to do things the way knowing it will work for sure. Why I didnt loop from 0 to 3? Why I used sizeof() instead? Well, I thought I could later on change this function to:

    void swap( u8 *val, u8 length );

    And replace sizeof() calls with length. That way I could call it:
    swap( (u8*)&myVar, sizeof( myVar ) );
    And this would work wonders for all my needs from 8bit to 64bit integers



    So why I post a question? I am sure theres allways more bright way to do things like this (maybe even optimized, faster ones... which I actually dont need) and am a bit interested... since this is really the best I can do atm.
    Last edited by Rennor; 09-05-2006 at 02:58 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    htonl and its kin perhaps?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Rennor's Avatar
    Join Date
    Aug 2006
    Location
    Finland
    Posts
    45
    Quote Originally Posted by quzah
    htonl and its kin perhaps?
    Sorry I forgot to mention I run my tests using VC++ 6.0.

    Or should I feel silly by being unable to find htonl, htons, ntohl, ntohs with my compiler?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You probably have them. Like you said though, there are plenty of ways to do the same thing, these are just some "not really standard but pretty common" functions which will work (if you have them that is).


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Monitoring Page Swapping?
    By Cell in forum Linux Programming
    Replies: 10
    Last Post: 06-13-2010, 12:16 PM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. C bubble sort?
    By fredanthony in forum C Programming
    Replies: 11
    Last Post: 02-13-2006, 09:54 PM
  4. swapping within a char pointer array
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 01:31 AM