What is a BCD code? BCD code. How can I convert a BCD code into a binary value?

Source: Internet
Author: User

Meaning: four-digit binary numbers are used to represent one digit in the value (0 ~ 9), BCD code for short, that is, BCD code.

It can also be understood that the decimal number is expanded into a binary number in the form of 8421 (14 is displayed as a binary number in the form of 8421, or 00010100 decimal number indicates 20)

The BCD code of 14 is 20.

The function is to change 20 to 14. How can we change 20 to 14? Starting from the definition, each bit is represented by four binary digits. Then add the first four digits * 10 of the byte to the last four digits. See the essence through phenomena. In this case.

 

The BCD code is a four-digit binary code, that is, converting a decimal number into a binary code, but it is a little different from a normal conversion, each decimal number 0-9 corresponds to a four-digit binary code. The relationship is as follows: decimal 0 corresponds to binary 0000; decimal 1 corresponds to binary 0001 ....... 9 1001 the next 10 has two of the above codes to indicate that 10 represents 00010000, that is, BCD code generates carry when it encounters 1001. Unlike normal binary codes, 1111 generates carry 10000 only when it reaches.

/*************************************** ***************************************
* Function: bcd2val
* Description: convert one byte packed BCD to binary value.
* Input: BCD; // one byte packed BCD.
* Return: binary value.
* Note: N/
* Globals changed: N/
**************************************** **************************************
*/
DWORD bcd2val (byte BCD)
{
Return (BCD> 4) * 10 + (BCD & 0x0f ));
}/* Bcd2val */

 

Bcd2val (20) =

(1), 00010100> 4 = 00000001

(2), 1*10 = 10

(3) BCD & 0x0f = 00000100

(4), 10 + 4 = 14 hexadecimal representation as E

 

Converts word data (BCD) into binary values.

DWORD bcd2bin (word bcdval)
{
Return (bcd2val (byte) (bcdval/256) * 100 + bcd2val (byte) (bcdval % 256 )));
}

Dividing by 256 is equivalent to moving the right eight bits, that is, converting the previous byte into a binary value, and converting the BCD code value of the last byte into a binary value.

 

It seems that there is a problem to write this function after BCD is converted to a binary value.
DWORD bcd2val (byte BCD)
{
Return (BCD> 4) * 10 + (BCD & 0x0f ));
 
}

 

If BCD code 20 is assigned, it seems that it is okay. The calculated value is 14.
If 241 is used for calculation. The calculated value is 151. Eg:

1. the decimal value of BCD code 241 is 151.

2. the decimal value of BCD code 256 is 100. The binary value is 0x64.

2011-06-01 WCG

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.