C # BCD encoding and decoding

Source: Internet
Author: User
Tags binary to decimal decimal to binary
(1) BCD code (Binary to decimal code) People are usually used to the decimal number, while the computer mostly uses binary to represent and process numerical data. Therefore, when the computer inputs and outputs data, the conversion from decimal to binary is required. Each digit of the decimal number is encoded in binary format. It is called the decimal number of binary encoding, that is, binary to decimal or BCD (Binary coded decimal) encoding. There are many BCD encoding methods, usually using 8421 encoding. This encoding method is the most natural and simple. The four-digit binary number is used to represent a decimal number. The weights of each digit from left to right are 23, 22, 21, and 20, respectively, that is, 8, 4, 2, and 1. For example, if the decimal number is 1975, the Code 8421 (d) = 1975 0001 1001 0111 (BCD) can be obtained. If the four-digit binary represents one decimal digit, there will be six more States, these redundant status codes are called invalid codes in BCD codes. The conversion between the BCD code and the binary code is not directly carried out. To convert the BCD code into a binary code, you must first convert the BCD code into a decimal code, and then convert it into a binary code; to convert a binary code to a BCD code, convert the binary code to a decimal code before converting it to a BCD code. During the Encoding Process, the number 69 is encoded in BCD (Note: BCD encoding is not followed by comments ). 1. convert 6, 9 into a binary representation: 6 (00000110) 9 (00001001). As you can see, the maximum number of 9 is only four digits, and four digits are wasted during transmission; 2. merge 69 into one byte and take the minimum 4 bits of the binary encoding respectively, put the lower four bits of 9 to the lower four bits of the first six to the back. The new byte binary encoding is 10010110. 3. after the encoding process is completed, 69 BCD encoding results are 10010110. Decoding process: decodes 69 BCD code 10010110. 1. split the 4-bit high and 4-bit low of 10010110 to obtain the 1001 and 0110 binary numbers. add the first four digits of 1001 and 0110 to 0000 respectively to obtain two 8-bit binary numbers:, and 3. because the encoding time is low, we arrange the two binary numbers in the order of 00000110 000010001; C # code implementation: int x = 97; /// encode the integer 97bcd byte M = (byte) 7) <4) + (byte) 9 )); // The obtained M is the string BS = ""; BS = (byte) (M <4)> 4 )). tostring () + "+" + (M> 4 ). tostring (); BS is the string obtained from the BCD code.
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.