BCD码与16进制互转算法
#include <stdio.h>
unsigned char BCD2HEX(unsigned int bcd_data
)
{
unsigned char temp
;
temp
=((bcd_data
>>8)*100)|((bcd_data
>>4)*10)|(bcd_data
&0x0f);
return temp
;
}
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
;
}
int main(void)
{
unsigned int temp
,temp1
;
temp
= BCD2HEX(11);
temp1
= HEX2BCD(0xb);
printf("temp:0x%x\n",temp
);
printf("temp1:0x%x\n",temp1
);
return 0 ;
}
运行结果:
temp:0xb
temp1:0x11
转载请注明原文地址:https://tech.qufami.com/read-10768.html