About encoding and decoding of BCD (C # Edition)

Source: Internet
Author: User
Tags binary to decimal decimal to binary

Recently in See PHS SMGP1.32 version of the agreement, see inside there is a BCD code, the Internet search a bit, to the PB version of more, I prefer to use C #, so to a C # version, hehe

I copied two notes from the Internet:

(1) BCD code (two to decimal code)
People are usually used to using decimal numbers, and the computer uses binary representation and processing of numerical data.
Therefore, when the computer input and output data, it is necessary to carry out the conversion from decimal to binary processing.

Each digit of the decimal number is written in binary form, called the binary encoded decimal number,
That is, binary to decimal encoding or BCD (Binary coded decimal) encoding.

BCD coding methods are many, usually using 8421 code, this encoding method is the most natural and simple.
Its method uses a four-bit binary number to represent a decimal number, from left to right each digit corresponds to the right respectively is
23, 22, 21, 20, that is 8, 4, 2, 1. For example, a decimal number 1975 of 8421 yards can be drawn

1975 (D) =0001 1001 0111 0101 (BCD)

Using a four-bit binary to represent a decimal number, there are 6 more states, which are called illegal codes in the BCD code.
The conversion between the BCD code and the binary is not done directly,
When the BCD code needs to be converted into binary code, the BCD code is converted into decimal code and then converted into binary code;
When you need to convert binary to BCD, you convert the binary to decimal code and then to BCD.

The encoding process that converts the number 69 to BCD (Note: The BCD code is low on the front and will no longer be commented after).

1. Convert 6, 9 to binary representation: 6 (00000110) 9 (00001001), you can see, the largest number 9 also as long as 4 bits, in the transmission process wasted 4 bits;

2. Merge 69 into one byte, Take 6, 92 low 4 digits, and put the lower 9 digits of four in the lower 6 position in front four, and then draw the new byte binary code 10010110;

3. Complete the coding process, 69 of the BCD code result is 10010110.

Decoding process: Decoding 69 of BCD code 10010110.

1. Separate the 10010110 high 4 digits from the lower 4 bits, and get two binary numbers 1001 and 0110;

2. A binary number of 0000-digit 00001001,00000110 is obtained by adding 4-bit two to the front of 1001 and 0110 respectively;

3. Because the coding is low before, so we will two binary number of the order is 00000110 000010001;

C # version Code implementation:

int x=97; To encode the integer 97BCD
Byte m= (((byte) 7) <<4) + (((byte) 9));
The resulting m is the number 97 and the BCD code byte representation

String Bs= "";
Bs= (((Byte) (m<<4)) >>4). ToString () + "+" + (m>>4). ToString ();

BS is the string that is extracted from the BCD code.

Finish

Related Article

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.