1. 十进制转换BCD码
#include <stdio.h>
int main(int argc, char **argv[])
{
int decimal = atoi(argv[1]);
int sum = 0, i;//i计数变量,sum返回的BCD码
for ( i = 0; decimal > 0; i++)
{
sum |= ((decimal % 10 ) << ( 4*i));
decimal /= 10;
}
printf("sum=%d\n",sum);
//return sum;
}
运行结果如下:
2. BCD码转换十进制
int BCD_Decimal(int bcd)
{
int Decimal;
Decimal=bcd>>4;
Decimal=Decimal*10+(bcd&=0x0F)
return Decimal;
}