1、把Ascii字符转16进制
/***************************
函数名:Ascii2Hex
功能描述:把Ascii字符转16进制
参数:Ascii字符
返回:16进制
***************************/
unsigned char Ascii2Hex( unsigned char bAscii )
{
unsigned char bHex = 0;
if( ( bAscii >= 0 ) && ( bChar <= 9 ) )
{
bHex = bAscii + 0x30;
}
else if( ( bAscii >= 10 ) && ( bAscii <= 15 ) )
{
bHex = bAscii + 0x37;
}
else
{
bHex = 0xff;
}
return bHex;
}
2、把16进制转Ascii字符
/***************************
函数名:Hex2Ascii
功能描述:把16进制转Ascii字符
参数:16进制
返回:Ascii字符
***************************/
unsigned char Hex2Ascii( unsigned char bHex )
{
unsigned char bAscii = 0;
if( ( bHex >= '0' ) && ( bHex <= '9' ) )
{
bAscii = bHex - '0';
}
else if( ( bHex >= 'A' ) && ( bHex <= 'F' ) )
{
bAscii = bHex - '7';
}
else if( ( bHex >= 'a' ) && ( bHex <= 'f' ) )
{
bAscii = bHex - 0x57;
}
else
{
bAscii = 0xff;
}
return bAscii;
}
3、把16进制转BCD值
/***************************
函数名:Hex2Bcd
功能描述:把16进制转BCD值
参数:16进制值
返回:BCD值
***************************/
u_8 Hex2Bcd( u_8 HexCode )
{
return( ( HexCode % 10 ) + ( HexCode / 10 * 16 ) );
}
4、把BCD值转2进制
/***************************
函数名:Bcd2Bin
功能描述:把BCD值转2进制
参数:BCD值
返回:2进制值
***************************/
u_8 Bcd2Bin( u_8 BcdCode )
{
return( ( ( ( BcdCode >> 4 ) & 0x0F ) * 10 ) + ( BcdCode & 0x0F ));
}
5、把2进制转BCD值
/***************************
函数名:Bin2BCD
功能描述:把2进制转BCD值
参数:2进制值
返回:BCD值
***************************/
u_8 Bin2BCD( u_8 BinCode )
{
return( ( ( (BinCode)/10 ) << 4 ) + ( (BinCode) %10 ));
}
6、把x值的第i位置为1
/********把x值的第i位置为1*************/
#define X_SETBIT( x , i ) ( (x) |= ( 0x1 << (i) ))
7、把x值的第i位清为0
/**********把x值的第i位清为0**********/
#define X_CLRBIT( x , i ) ( (x) &= ( ~( 0x01 << (i) )))
8、判断x值的第i位是否为1
/*********判断x值的第i位是否为1**********/
#define X_CHKBIT( x , i ) ( ((x) >> (i) ) & 0x01 )
9、x,y值中的最小值
/*********x,y值中的最小值**********/
#define X_MIN( x ,y ) (((x)>(y))?(y):(x))
10、x,y值中的最大值
/*********x,y值中的最大值**********/
#define X_MAX( x ,y ) (((x)>(y))?(x):(y))
11、取i值的低8位
/*********取i值的低8位**********/
#define GET_4_BYTE(i) (u_8)( ( (i) & 0xFF ) >> 0 )
12、取i值的8-16位
/*********取i值的8-16位**********/
#define GET_3_BYTE(i) (u_8)( ( (i) & 0xFF00 ) >> 8 )
13、取i值的16-24位
/*********取i值的16-24位**********/
#define GET_2_BYTE(i) (u_8)( ( (i) & 0xFF0000 ) >> 16 )
14、取i值的24-32位
/*********取i值的24-32位**********/
#define GET_1_BYTE(i) (u_8)( ( (i) & 0xFF000000 ) >> 24 )
15、4个字节为int型变量
/*********4个字节为int型变量**********/
#define BYTE_TO_INT( byte,byte1,byte2,byte3 ) ( (byte<<24) | (byte1 << 16) | (byte2 << 8) | byte3 )
16、2个字节为short型变量
/*********2个字节为short型变量**********/
#define BYTE_TO_SHORT( byte ,byte1) ( (byte<<8) | byte1 )
17、int型变量为4个字节
/*********int型变量为4个字节**********/
#define INT_TO_BYTE( Val,byte ) do{ (byte)[0] = ((Val)>>24)&0xff;\
(byte)[1] = ((Val)>>16)&0xff;\
(byte)[2] = ((Val)>>8)&0xff;\
(byte)[3] = ((Val))&0xff;\
}while(0)
18、short型变量为2个字节
/*********short型变量为2个字节**********/
#define SHORT_TO_BYTE( Val ,byte) do{ (byte)[0] = ((Val)>>8)&0xff;\
(byte)[1] = (Val)&0xff;\
}while(0)
19、标准时间time转换字符串输出
/*********标准时间time转换字符串输出**********/
#define TIME_2_STR(t, str) do{ tm_t tmtime;\
gmtime_r( (time_t *)&t,&tmtime );\
sprintf(str,"%04d-%02d-%02d %02d:%02d:%02d",1900 + tmtime.tm_year, tmtime.tm_mon + 1, \
tmtime.tm_mday,tmtime.tm_hour, tmtime.tm_min, tmtime.tm_sec);\
}while(0)
20、IP值转换IP字符串输出
/*********IP值转换IP字符串输出**********/
#define IPADDR_2_STR(lIp, str) do{ u_8 ipaddr[4];\
memcpy (&(ipaddr[0]), &(lIp), 4);\
sprintf(str,"%03d.%03d.%03d.%03d",ipaddr[0],ipaddr[1],ipaddr[2],ipaddr[3]);\
}while(0)