zoukankan      html  css  js  c++  java
  • 字节顺序


    #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( "
    " );
    
    }
    
    
    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;
    
    }


  • 相关阅读:
    PHP pcntl
    Linux 远程登录命令telnet
    git .gitignore不生效
    使用 GoLand 启动 运行 Go 项目
    Go语言: 万物皆异步
    MYSQL 单表一对多查询,将多条记录合并成一条记录
    详解PHP中instanceof关键字及instanceof关键字有什么作用
    all_user_func()详解
    python的反射
    python 的魔术方法
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6763660.html
Copyright © 2011-2022 走看看