#include #include int byteswap( void *swappee, int Nbytes ) { char temp, *ptr = swappee ; if( Nbytes == 2 ){ temp = ptr[0] ; ptr[0] = ptr[1] ; ptr[1] = temp ; return 0 ; } if( Nbytes == 4 ){ temp = ptr[0] ; ptr[0] = ptr[3] ; ptr[3] = temp ; temp = ptr[1] ; ptr[1] = ptr[2] ; ptr[2] = temp ; return 0 ; } if( Nbytes == 8 ){ temp = ptr[0] ; ptr[0] = ptr[7] ; ptr[7] = temp ; temp = ptr[1] ; ptr[1] = ptr[6] ; ptr[6] = temp ; temp = ptr[2] ; ptr[2] = ptr[5] ; ptr[5] = temp ; temp = ptr[3] ; ptr[3] = ptr[4] ; ptr[4] = temp ; return 0 ; } return -1 ; } int main() { float f; unsigned char cbuf[4]; unsigned char *p; cbuf[0] = 0xff; cbuf[1] = 0xae; cbuf[2] = 0x47; cbuf[3] = 0xc5; printf("%x %x %x %x --> ", (unsigned char)cbuf[0], (unsigned char)cbuf[1], (unsigned char)cbuf[2], (unsigned char)cbuf[3]); f = *((float *)cbuf); byteswap((void *)(&f), 4); p = (unsigned char *)&f; printf("%x %x %x %x\n", *p, *(p + 1), *(p + 2), *(p + 3)); return 0; }