Dear friends,
( Suppose we change byte orders. For example, our program
reads a file as a character string and converts the byte order. )
// a simple code
#define INTEL_CPU
#ifdef INTEL_CPU
/* *************************** FlipLong() ******************************
* Converts a long in "Big Endian" format (ie, Motorola 68000) to Intel
* reverse-byte format, or vice versa if originally in Big Endian.
************************************************** *******************
*/
void FlipLong(unsigned char * ptr) {
register unsigned char val;
/* Swap 1st and 4th bytes */
val = *(ptr);
*(ptr) = *(ptr+3);
*(ptr+3) = val;
/* Swap 2nd and 3rd bytes */
ptr += 1;
val = *(ptr);
*(ptr) = *(ptr+1);
*(ptr+1) = val;
}
#endif
----------------------------------------****** OR *****-------------------------------------------------
A more efficient function can be implemented using bitwise operations as
shown below:
unsigned long ByteSwap2 (unsigned long nLongNumber)
{
return (((nLongNumber&0x000000FF)<<24)+((nLongNumber&0x00 00FF00)<<8)+
((nLongNumber&0x00FF0000)>>8)+((nLongNumber&0xFF00 0000)>>24));
}
(Both the above samples are declares val/nLongNumber as unsigned char. )
---
Tell me how to deal with sign, or give me sample code
to convert byte orders with signed char.
please any one help me out.
regards,
velpur