BCD and ASCII conversion and BCD to int function, re-write, it feels good.
You can change the return value as needed.
/*********************** Stdfunc. h ***********************/
# Ifndef _ std_func_h _
# DEFINE _ std_func_h _
/*
Parameters
BCD: converted BCD result
ASC: the ASC string to be converted
Len: length to be converted
FMT: converted format, that is, if the length is an odd number, add the value before or after zero.
Return Value
Length of BCD bytes processed
*/
Int asc2bcd (unsigned char * BCD, const char * ASC, int Len, int FMT );
/*
Parameters
ASC: ASC result after conversion
BCD: The BCD string to be converted
Len: length to be converted
FMT: the format of BCD to be converted, that is, if the length is an odd number, it must be supplemented before or after completion.
Return Value
Length of BCD bytes processed
*/
Int bcd2asc (char * ASC, const unsigned char * BCD, int Len, int FMT );
/*
Parameters
Plen: int result pointer after conversion
BCD: The BCD string to be converted
Len: length to be converted
FMT: the format of BCD to be converted, that is, if the length is an odd number, it must be supplemented before or after completion.
Return Value
Length of BCD bytes processed
*/
Int bcd2int (int * Plen, const unsigned char * BCD, int Len, int FMT );
# Endif/* _ std_func_h _*/
/*********************** Stdfunc. c ***********************/
# Include "stdfunc. H"
Int asc2bcd (unsigned char * BCD, const char * ASC, int Len, int FMT)
{
Int I, odd;
Char C;
Odd = Len & 0x01;
If (odd &&! FMt)
* BCD ++ = (* ASC ++) & '/x0f ';
Len> = 1;
For (I = 0; I <Len; I ++ ){
C = (* ASC ++) <4;
C | = (* ASC ++) & '/x0f ';
* BCD ++ = C;
}
If (odd & FMT)
* BCD = (* ASC) <4;
Return (I + ODD );
}
Int bcd2asc (char * ASC, const unsigned char * BCD, int Len, int FMT)
{
Int I, odd;
Unsigned char C;
Odd = Len & 0x01;
If (odd &&! FMt)
* ASC ++ = (* BCD ++) & '/x0f') + '0 ';
Len> = 1;
For (I = 0; I <Len; I ++ ){
C = * BCD ++;
* ASC ++ = (C> 4) + '0 ';
* ASC ++ = (C & '/x0f') + '0 ';
}
If (odd & FMT)
* ASC = (* BCD)> 4) + '0 ';
Return (I + ODD );
}
Int bcd2int (int * Plen, const unsigned char * BCD, int Len, int FMT)
{
Int L = 0, I, odd;
Unsigned char C;
Odd = Len & 0x01;
If (odd &&! FMt)
L = (* BCD ++) & '/x0f ';
Len> = 1;
For (I = 0; I <Len; I ++ ){
C = * BCD ++;
L * = 10;
L + = C> 4;
L * = 10;
L + = C & '/x0f ';
}
If (odd & FMT)
{
L * = 10;
L + = (* BCD)> 4;
}
* Plen = L;
Return (I + ODD );
}