Conversion between BCD and ASCII and conversion from BCD to int

Source: Internet
Author: User

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 );
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.