|
发表于 2003-11-19 22:00:18
|
显示全部楼层
好像只有x86特殊吧
这里有一段W. Richard Stevens的代码,用于判断字节序
/*
* gcc -Wall -pipe -O3 -s -o byte_order byte_order.c
*/
#include <stdio.h>
#include <stdlib.h>
/*
* return value:
* 1 big-endian
* 2 little-endian
* 3 unknow
* 4 sizeof( unsigned short int ) != 2
*/
static int byte_order ( void )
{
union
{
unsigned short int s;
unsigned char c[ sizeof( unsigned short int ) ];
} un;
un.s = 0x0201;
if ( 2 == sizeof( unsigned short int ) )
{
if ( ( 2 == un.c[0] ) && ( 1 == un.c[1] ) )
{
puts( "big-endian" );
return( 1 );
}
else if ( ( 1 == un.c[0] ) && ( 2 == un.c[1] ) )
{
puts( "little-endian" );
return( 2 );
}
else
{
puts( "unknow" );
return( 3 );
}
}
else
{
printf( "sizeof( unsigned short int ) = %u\n", ( unsigned int )sizeof(unsigned short int ) );
return( 4 );
}
return( 3 );
} /* end of byte_order */
int main ( int argc, char * argv[] )
{
printf( "byte_order() = %d\n", byte_order() );
return( EXIT_SUCCESS );
} /* end of main */ |
|