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:
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: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; }
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.