What is a BCD code?

Source: Internet
Author: User

BCD code (Binary-coded decimal) is also known as binary decimal or decimalCode. Use four-digit binary numbers to represent 0 ~ 9. It is a binary digital encoding form that uses the binary-encoded decimal code. BCD code uses four digits to store a decimal number, so that the conversion between binary and decimal can be quickly carried out. This encoding technique is most commonly used in the design of the accounting system, because the accounting system often needs to accurately calculate a long number string. Compared to the general floating-point memory type, BCD code can be used to save the precision of the value, but can avoid the time consumed by the computer for floating-point operations. BCD encoding is also commonly used for other computation that requires high accuracy.

Source: http://baike.baidu.com/view/45179.htm

 

BCD encoding and decodingThe function is as follows:

 ///   <Summary>  ///  BCD Decoding  ///   </Summary>  ///   <Param name = "B"> </param> ///   <Returns> </returns>  Public   Static   Byte Unpackbcd ( Byte  B ){  //  Senior Four      Byte B1 = ( Byte ) (B> 4  );  //  Four lower      Byte B2 = ( Byte ) (B & 0x0f  );  Return ( Byte ) (B1 * 10 + B2 );}  ///   <Summary>  ///  BCD Encoding  ///   </Summary>  ///   <Param name = "B"> </param> ///   <Returns> </returns>  Public   Static   Byte Packbcd ( Byte  B ){  //  Senior Four      Byte B1 = ( Byte ) (B/ 10  );  //  Four lower      Byte B2 = (Byte ) (B % 10  );  Return ( Byte ) (B1 < 4 ) | B2 );} 

 

BCD EncodingThe Code is as follows:

 Class  Program {  //  BCD Encoding      Public   Static   Byte Packbcd (Byte  B ){  //  Senior Four          Byte B1 = ( Byte ) (B/ 10  );  //  Four lower          Byte B2 = ( Byte ) (B % 10  );  Return ( Byte ) (B1 < 4 ) | B2 );}  Static   Void Main ( String  [] ARGs ){  Byte [] Buff = New   Byte [ 2  ]; Datetime date = Datetime. now;  Byte In_month = ( Byte ) (Date. month );  Byte In_day = ( Byte  ) (Date. Day); console. writeline (in_month.tostring () + "    " + In_day.tostring (); buff [  0 ] = Packbcd (in_month); buff [  1 ] = Packbcd (in_day); console. writeline (buff [  0 ]. Tostring () + "   " + Buff [ 1  ]. Tostring (); console. readkey ();}} 

The result is as follows:

It can be seen that the BCD encoding in month 4 is not changed, and the BCD encoding in month 23 is changed to 35.

 

BCD DecodingThe Code is as follows:

 Class  Program {  //  BCD Decoding      Public   Static   Byte Unpackbcd ( Byte  B ){ //  Senior Four          Byte B1 = ( Byte ) (B> 4  );  //  Four lower          Byte B2 = ( Byte ) (B & 0x0f  );  Return ( Byte ) (B1 * 10 +B2 );}  Static   Void Main ( String  [] ARGs ){  Byte [] Buff = New   Byte [ 2 ] { 0x04 , 0x23  }; Console. writeline (buff [  0 ]. Tostring () + "   " + Buff [ 1  ]. Tostring ());  Byte In_month = unpackbcd (buff [ 0  ]);  Byte In_day = unpackbcd (buff [ 1  ]); Console. writeline (in_month.tostring () + "    " + In_day.tostring (); console. readkey ();}} 

The result is as follows:

It can be seen that after the BCD decoding on month 4 is not changed, and after the BCD decoding on 35, it is restored to the 23th day.

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.