BCD码与十进制相互转换

#include "stdio.h"


char BCD2DEC(unsigned char *bcd)
{
    unsigned char tmp = 0;
    if(*bcd>0x99)
      return -1;
    tmp = ((*bcd>>4)*10)+(*bcd&0x0F);
    *bcd = tmp;
    return 0;
}

char DEC2BCD(unsigned char *dec)
{
    unsigned char tmp = 0;
    if(153<*dec)
    return -1;
    tmp = ((*dec/10)<<4)|(*dec%10);
    *dec = tmp;
    return 0;
}


void main()
{
    unsigned char tmp=0x68;
    unsigned char tmp1 = 68;
    BCD2DEC(&tmp);
    DEC2BCD(&tmp1);
    printf("%d\n",tmp);
    printf("%X\n",tmp1);
}


 

你可能感兴趣的:(C,c语言)