C hexadecimal to decimal

Source: Internet
Author: User

Taking into account the opposite, here the complete code, by the way, can be considered to implement R hexadecimal and decimal conversion.

Complete code:

1 # include <stdio. h> 2 # include <math. h> 3 4 double 5 hextodecimal (INT); 6 7 Double 8 octtodecimal (INT); 9 10 double11 todecimal (INT, Int, INT); 12 13 int14 main (void) {15 int A = 0x3c; 16 double r = hextodecimal (a); 17 printf ("%. f \ n ", R); 18 A = 017; 19 R = octtodecimal (a); 20 printf (" %. f \ n ", R); 21} 22 23 double24 hextodecimal (int hex) {25 return todecimal (Hex, 15, 4); 26} 27 double28 octtodecimal (INT Oct) {29 return todecimal (Oct, 7, 3); 30} 31 32 33 double34 todecimal (INT num, int base, int offset) {35 int I = 0; // I power of (B. 36 double r = 0; // decimal result. 37 Double B = base + 1; // The base number is B .38 while (Num) {39 double T = (INT) (Num & base ); 40 r + = T * POW (B, I ++); 41 num >>= offset; 42} 43 44 return r; 45}

The algorithm is quite simple and easy to understand. for example, in hexadecimal 3c, the numbers are sequentially obtained from right to left. The size of the binary value of C is 12 through & (and) 15 (4 bits 1. I don't need to talk about it anymore. it's easy.

C hexadecimal to decimal

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.