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