字节顺序


#include <stdio.h>

typedef int* int_ptr;
typedef unsigned char* byte_ptr;


void show_bytes( byte_ptr start, int len ){

	int i;

	for( i = 0; i < len; ++i ){
		printf( " %4.2x", start[i] );
	}

	printf( "\n" );

}


void show_int( int x ){
	show_bytes( ( byte_ptr ) &x, sizeof( int ) );
}


void show_float( float x ){
	show_bytes( ( byte_ptr ) &x, sizeof( float ) );
}


void show_ptr( void* x ){
	show_bytes( ( byte_ptr ) &x, sizeof( void* ) );
}


void test_show_bytes( int val ){

	int ival   = val;
	float fval = ( float ) ival;
	int* pval  = &ival;

	show_int( ival );
	show_float( fval );
	show_ptr( pval );

}


int main(){

	/*
		Machine | value   | type  | byte( hexadecimal ) |
		-------------------------------------
		Linux   | 12345   | int   | 39 30 00 00
		NT      | 12345   | int   | 39 30 00 00
		Sun     | 12345   | int   | 00 00 30 39 ( big endian )
		Alpha   | 12345   | int   | 39 30 00 00
		--------------------------------------
		Linux   | 12345.0 | float | 00 e4 40 46
		NT      | 12345.0 | float | 00 e4 40 46
		Sun     | 12345.0 | float | 46 40 e4 00
		Alpha   | 12345.0 | float | 00 e4 40 46
		--------------------------------------
		Linux   | &ival   | int*  | 3e fa ff bf
		NT      | &ival   | int*  | 1c ff 44 02
		Sun     | &ival   | int*  | ef ff fc e4
		Alpha   | &ival   | int*  | 80 fc ff 1f 01 00 00 00
	*/

	test_show_bytes( 12345 );

	return 0;

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值