C语言高低字节转换
# include <iostream>
# include <stdint.h>
# include <stdio.h> # define _G_HI24_BYTE ( x) ( ( ( x) >> 24 ) & 0xFF )
# define _G_HI16_BYTE ( x) ( ( ( x) >> 16 ) & 0xFF )
# define _G_HI8_BYTE ( x) ( ( ( x) >> 8 ) & 0xFF )
# define _G_HI_BYTE ( x) ( ( x) & 0xFF )
# define GetBit ( Number, pos) ( ( Number >> pos) & 1 )
# define SetBit ( Number, pos) ( ( Number) |= ( 1 << pos) )
# define ClrBit ( Number, pos) ( ( Number) &= ~ ( 1 << pos) )
# define FanBit ( Number, pos) ( ( Number) ^= ( 1 << pos) ) using namespace std;
int main1 ( ) ; int main ( )
{ uint16_t checksum = 257 ; uint8_t ck[ 2 ] = { 0 } ; ck[ 0 ] = ( uint8_t ) ( checksum & 0xFF ) ; ck[ 1 ] = ( uint8_t ) ( checksum >> 8 ) ; printf ( "ck[0]:%d ck[1]:%d\n" , ck[ 0 ] , ck[ 1 ] ) ; ck[ 0 ] = _G_HI_BYTE ( checksum) ; ck[ 1 ] = _G_HI8_BYTE ( checksum) ; printf ( "_G_HI_BYTE ck[0]:%d ck[1]:%d\n" , ck[ 0 ] , ck[ 1 ] ) ; ck[ 0 ] = 0x01 ; ck[ 1 ] = 0x01 ; uint16_t checksum1 = ck[ 0 ] | ck[ 1 ] << 8 ; printf ( "checksum1:%d\n" , checksum1) ; uint16_t checksum2 = ck[ 0 ] + ( ck[ 1 ] << 8 ) ; printf ( "checksum2:%d\n" , checksum2) ; uint8_t num = 0xAA ; for ( int i = 0 ; i < 8 ; i++ ) { printf ( "GetBit%d:%x\n" , i, GetBit ( num, i) ) ; } num = 0 ; for ( int i = 0 ; i < 8 ; i++ ) { printf ( "ClrBit%d:%x\n" , i, SetBit ( num, i) ) ; } return 0 ;
}