Recursively convert a decimal number to any hexadecimal number

Source: Internet
Author: User

Author: Qing Dujun


Convert a decimal number to any hexadecimal number (2 ~ Between hexadecimal )~

C language:

# Include
 
  
// N: original number (decimal) to: convert to the hexadecimal buf: Save the converted hexadecimal number void fn (int n, int to, char * buf) {// define I as static purpose: to use the same I value static int I = 0 in multi-layer recursion; // in special cases, 0, processing independently if (n = 0) {buf [I] = '0'; return;} // preprocessing of negative numbers if (n <0) {buf [I] = '-'; ++ I; n =-n;} // recursion of positive integers for hexadecimal conversion. if (n! = 0) {// recursive into the stack fn (n/to, to, buf); // recursive elastic stack switch (n % to) {case 15: buf [I] = 'F'; break; case 14: buf [I] = 'E'; break; case 13: buf [I] = 'D'; break; case 12: buf [I] = 'C'; break; case 11: buf [I] = 'B'; break; case 10: buf [I] = 'a '; break; default: buf [I] = (n % to + '0'); break;} + + I ;}} int main () {char buf [50] = ""; int n, to; scanf ("% d", & n, & to); // convert fn (n, to, buf); puts (buf); return 0 ;}
 
C ++ version, optimized the program and reduced some code ~~~

It turns out that when I thought static would define the data type, the right value must be a const value. Of course, this is also true when I tested it in the C environment.

Example:

1) static int a = 0;

2) static int a = (1 = 2 )? 1: 3;×

However, today I can see that the second form is correct. Later I asked, it turned out to be a. cpp suffix.

During recursion, some execution statements are put into the static statement. Because the static statement is executed only once and some are put in the statement, it is better to execute only once ~~~~

The following code is displayed in. CppFile,. CAn error is reported ~~~

# Include
 
  
// N: original number (decimal) to: convert to the hexadecimal char * fn (int n, int to) {// define the purpose of static variables: to use the same value in multi-layer recursion, and perform static char buf [100] = "", BinHex [] = {'0', '1 ', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B ', 'C', 'D', 'E', 'F'}; // handle n = 0 and n <0, static: Only one static int I = (n> = 0 )? (Buf [0] = '0', 0): (buf [0] = '-', n =-n, 1); // positive integer recursion, perform hexadecimal conversion. if (n! = 0) {// recursive into the stack fn (n/to, to); // recursive pop-up stack buf [I ++] = BinHex [n % to];} return buf;} int main () {char * buf = NULL; int n, to; // input n: original data: the hexadecimal scanf ("% d", & n, & to) to be converted; // The hexadecimal conversion buf = fn (n, ); puts (buf); return 0 ;}
 
Of course, the above Code does not process more than hexadecimal and less than binary ~~~~


References: SnailSet blog column, converts decimal integers into B-hexadecimal strings (recursive and non-recursive implementations), http://blog.csdn.net/snailset/article/details/26492715


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.