Hexadecimal, BCD, and decimal conversion

Source: Internet
Author: User




In the design of embedded software, we often encounter hexadecimal,
BCD
The conversion between the Code and the decimal number.
M1
Card applications involve a large number of hexadecimal,
BCD
Conversion between code and decimal. The author
BCD
Code, hexadecimal
Rights can be easily exchanged between them.

 # Include <stdio. h> <br/> # include <string. h> </P> <p> /////////////////////////////// /// // <br/> // function: binary inversion <br/> // input: const unsigned char * SRC binary data <br/> // int length the length of the binary data to be converted <br/> // output: unsigned char * the binary data after DST is reversed <br/> // return: 0 success <br/> //////////////////////////// /// // <br/> int convert (unsigned char * DST, const Unsigned char * SRC, int length) <br/>{< br/> int I; </P> <p> for (I = 0; I <length; I ++) <br/>{< br/> DST [I] = SRC [I] ^ 0xff; <br/>}< br/> return 0; <br/>}</P> <p> //////////////////////////// /// // <br/> // function: convert hexadecimal to decimal <br/> // input: const unsigned char * hex hexadecimal data to be converted <br/> // int length hexadecimal Data Length <br/> // output: <br/> // return: decimal data after int rslt conversion <br/> // <br/> // Train of thought: the range of the decimal number represented by each character bit in hexadecimal format is 0 ~ 255, in hexadecimal format: 256 <br/> // shifts 8 bits to the left (<8) equivalent value multiplied by 256 <br/> /////////////////////////// //// // <br/> unsigned long hextodec (const unsigned char * hex, int length) <br/>{< br/> int I; <br/> unsigned long rslt = 0; </P> <p> for (I = 0; I <length; I ++) <br/> {<br/> rslt + = (unsigned long) (hex [I]) <(8 * (length-1-i )); </P> <p >}</P> <p> return rslt; <br/>}</P> <p> //////////////////////////// ///// /// // <Br/> // function: convert decimal to hexadecimal <br/> // input: int dec decimal data to be converted <br/> // length of the hexadecimal data after int length conversion <br/> // output: unsigned char * hex converted hexadecimal data <br/> // return: 0 success <br/> // train of thought: the principle is the same as that in hexadecimal notation <br/> ///////////////////////////// /// // <br/> int dectohex (INT Dec, unsigned char * hex, int length) <br/>{< br/> int I; </P> <p> for (I = length-1; I >= 0; I --) <br/>{< br/> hex [I] = (Dec % 256) & 0xff; <br/> DEC/= 256; <br/>}</P> <p> return 0; <br/>}</P> <p> //////////////////////////// /// // <br/>/ /function: request permission <br/> // enter: int base number <br/> // int times weight level <br/> // output: <br/> // return: unsigned long current data bit permission <br/> //////////////////////// /// // <br/> unsigned long power (INT base, int times) <br/>{< br/> int I; <br/> unsigned long rslt = 1; </P> <p> for (I = 0; I <times; I ++) <br/> rslt * = base; </P> <p> return rslt; <br/>}</P> <p> //////////////////////////// /// // <br/>/ /function: convert BCD to 10 hexadecimal <br/> // enter: const unsigned char * BCD code to be converted <br/> // int length BCD code Data Length <br/> // output: <br/> // return: Unsigned long current data bit permission <br/> // <Br/> // train of thought: compress the BCD code in decimal format from 0 ~ 99, decimal: 100 <br/> // first, calculate the decimal value represented by each character, then multiply the value by <br/> ///////////////////////////////// //////////////////// <br/> unsigned long bcdtodec (const unsigned char * BCD, int length) <br/>{< br/> int I, TMP; <br/> unsigned long dec = 0; </P> <p> for (I = 0; I <length; I ++) <br/>{< br/> TMP = (BCD [I]> 4) & 0x0f) * 10 + (BCD [I] & 0x0f); <br/> dec + = TMP * power (100, length-1-i ); <br/>}</P> <p> return Dec; <br/>}</P> <p> //////////////////////////// /// // <br/>/ /function: decimal to BCD code <br/> // enter: int dec decimal data to be converted <br/> // int length BCD code Data Length <br/> // output: unsigned char * BCD converted BCD code <br/> // return: 0 success <br/> // train of thought: same principle as BCD code to decimal <br/> //////////////////////// /// // <br/> int dectobcd (INT Dec, unsigned char * BCD, int length) <br/>{< br/> int I; <br/> int temp; </P> <p> for (I = length-1; I> = 0; I --) <br/> {<br/> temp = dec % 100; <br/> BCD [I] = (temp/10) <4) + (TEMP % 10) & 0x0f); <br/> DEC/= 100; <br/>}</P> <p> return 0; <br/>}< br/> // --------------- function information -------------------------------------- <br/> // number of letters: bcdtodec <br/> // Function Description: Convert the BCD code to a decimal code <br/> // entry parameter: temp: converted BCD code <br/> // return: converted decimal code <br/> // OPERATOR: <br/> // Date: 2006.11.24 <br/> // description: <br/> // optional <br/> uint8 bcdtodec (uint8 temp) <br/>{< br/> return (temp/16) * 10 + TEMP % 16); <br/>}</P> <p> // ------------- function information ------------------------------------------ <br/> // number of letters: dectobcd <br/> // Function Description: Convert hexadecimal code to BCD code. <br/> // entry parameter: temp: converted decimal code <br/> // return: converted BCD code <br/> // OPERATOR: <br/> // Date: 2006.11.24 <br/> // Description: <br/> // optional <br/> uint8 dectobcd (uint8 temp) <br/>{< br/> return (temp/10) * 16 + TEMP % 10); <br/>}< br/> 

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.