Re: [Algorithms] Byte Swapping
Brought to you by:
vexxed72
|
From: Michael S. H. <mic...@ud...> - 2000-07-17 23:18:40
|
Just for good measure, here's ours
/*
useful macros for swapping 16 and 32 bit data types
*/
#define MAKE16(l, h) ((Uint16)(((Byte)(l)) | ((Uint16)((Byte)(h))) << 8))
#define MAKE32(l, h) ((Uint32)(((Uint16)(l)) | ((Uint32)((Uint16)(h))) << 16))
#define LO16(l) ((Uint16)(l))
#define HI16(l) ((Uint16)(((Uint32)(l) >> 16) & 0xFFFF))
#define LO8(w) ((Byte)(w))
#define HI8(w) ((Byte)(((Uint16)(w) >> 8) & 0xFF))
/* these assume that the swapping is from motorola to intel format */
#if __cplusplus
#if NEEDBYTESWAP
inline Int16 ByteSwap( Int16 &x )
{
Uint16 y = (Uint16)x;
y = (Uint16)(y >> 8 | (y << 8 & 0xff00));
x = (Int16 &)y;
return x;
}
inline Uint16 ByteSwap( Uint16 &x )
{
x = (Uint16)(x >> 8 | (x << 8 & 0xff00));
return x;
}
inline Int32 ByteSwap( Int32 &x )
{
Uint32 y = (Uint32)x;
y = y >> 24 | (y >> 8 & 0xff00) | (y << 8 & 0xff0000) | (y << 24 & 0xff000000);
x = (Int32 &)y;
return x;
}
inline Uint32 ByteSwap( Uint32 &x )
{
x = x >> 24 | (x >> 8 & 0xff00) | (x << 8 & 0xff0000) | (x << 24 & 0xff000000);
return x;
}
template<class T> inline T ByteSwap( T &parm )
{
Uint32 x = (Uint32)parm;
x = x >> 24 | (x >> 8 & 0xff00) | (x << 8 & 0xff0000) | (x << 24 & 0xff000000);
parm = (T &)x;
return parm;
}
inline float ByteSwap( float &x )
{
Uint32 * pTemp = (Uint32*)&x;
Uint32 y = *pTemp;
y = y >> 24 | (y >> 8 & 0xff00) | (y << 8 & 0xff0000) | (y << 24 & 0xff000000);
pTemp = (Uint32*)&y;
x = *((float*)pTemp);
return x;
}
inline double ByteSwap( double &x )
{
Uint32 * pParm = (Uint32*)&x;
Uint32 nFirst = pParm[0];
Uint32 nSecond = pParm[1];
ByteSwap( nFirst );
ByteSwap( nSecond );
pParm[0] = nSecond;
pParm[1] = nFirst;
return x;
}
#else
#define ByteSwap(x) (x)
#endif
#endif /* __cplusplus */
At 03:41 PM 7/17/00, you wrote:
>Jumping in late : I'm sure you don't need it now, but
>here's a crazy ANSI C byte-swapper :
>
>let x = a 32 bit ulong , ABCD
>then y = DCBA
>
>y = (x = x>>16 | x<<16) ^ (x = (x^x>>8) & 0x00FF00FF) ^ (x<<8);
>
>At 11:45 PM 7/15/2000 -0700, you wrote:
>>I can do the obvious method, but I was wondering if anyone have any
>>cool/fast code snippets for byte swapping integers and floats?
>>
>>Tom
>>
>>
>>_______________________________________________
>>GDAlgorithms-list mailing list
>>GDA...@li...
>>http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list
>>
>>
>--------------------------------------
>Charles Bloom www.cbloom.com
>
>
>_______________________________________________
>GDAlgorithms-list mailing list
>GDA...@li...
>http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list
|