hex转bcd
typedef unsigned char uint_8;
typedef unsigned short int uint_16;
#define hydrology_8 uint_8
#define hydrology_16 uint_16
hydrology_8 HexToBcd(hydrology_8 bcd[],hydrology_8 hex[],hydrology_8 hex_len)
{
hydrology_8 len=0;
double fNum = 0;
char cNum[16];
hydrology_8 cNumLen =0;
hydrology_8 *pbcd = bcd;
char *pCNum = cNum;
for (hydrology_8 i = 0; i < hex_len; i++)
{
fNum *=0x100;
fNum +=hex[i];
}
sprintf(cNum,"%.0lf",fNum);
cNumLen = strlen(cNum);
len = cNumLen/2;
if(cNumLen%2)
{
*pbcd = (cNum[0]-'0') & 0x0f;
pbcd++;
cNumLen--;
pCNum++;
len ++;
}
while(cNumLen>0)
{
*pbcd = ((*pCNum)-'0')<<4 |((*(pCNum+1))-'0');
pbcd +=1;
pCNum +=2;
cNumLen -=2;
}
return len;
}
int main()
{
hydrology_8 buf[16];
hydrology_8 buf_len=0;
hydrology_8 hex[16]={0x98,0x12,0x34,0x19};
buf_len= HexToBcd(buf,hex,4);
for (int i = 0; i < buf_len; i++)
{
printf("%x\t",buf[i]);
}
system("pause");
}