- UID
- 872235
|
码制转换程序
#include
#include
#include
#include
/**此宏定义摘自51bbs Youth发表***/
#define LongToBin(n) \
(\
((n >> 21) & 0x80) | \
((n >> 18) & 0x40) | \
((n >> 15) & 0x20) | \
((n >> 12) & 0x10) | \
((n >> 9) & 0x08) | \
((n >> 6) & 0x04) | \
((n >> 3) & 0x02) | \
((n ) & 0x01) \
)
#define Bin(n) LongToBin(0x##n##l)
/********** HEX转BCD******/
/***bcd_data(<0x255,>0)***/
unsigned char BCD2HEX(unsigned int bcd_data)
{
unsigned char temp;
temp=((bcd_data>>8)*100)|((bcd_data>>4)*10)|(bcd_data&0x0f);
return temp;
}
/********** HEX转BCD******/
/***hex_data(<0xff,>0)****/
unsigned int HEX2BCD(unsigned char hex_data)
{
unsigned int bcd_data;
unsigned char temp;
temp=hex_data%100;
bcd_data=((unsigned int)hex_data)/100<<8;
bcd_data=bcd_data|temp/10<<4;
bcd_data=bcd_data|temp%10;
return bcd_data;
}
void main(void)
{
unsigned int c;
c= Bin(10101001); // then c = 0xA9
c=BCD2HEX(0x255); file://255 转成HEX为0xff
c=HEX2BCD(0xff); file://0xff 转成BCD码为 255
} |
|