16 or 16---> 10
Idea: The hexadecimal string is treated as a decimal string, followed by a method of converting a decimal string to a number, from a high to a position-by-character basis.
Code:
#include <stdio.h> #include <string.h>/* the value of M as needed to handle the 16-bit characters of more digits */#define M 10/* processing string, returning 10 binary digits */long long Toten (char s[]) {int I;/*long long to accommodate a larger number, if the data is too large, it is necessary to use array processing */long long d=0; for (i=0; s[i]; i++) {/ * d = d * 10 increases the number of bits in D, leaving a position for the currently processed character */ d=d*16; /* if s[i] is 0 ~ 9 digits, directly plus s[i]-' 0 ' * /if (s[i]>= ' 0 ' && s[i]<= ' 9 ') d=d+s[i]-' 0 '; /* Letters to be case-sensitive * /else if (s[i]>= ' a ' && s[i]<= ' F ') { d=d+s[i]-' a ' +10; } else if (s[i]>= ' a ' && s[i]<= ' F ') { d=d+s[i]-' a ' +10; } } return D;} int main () {char str[m];gets (str);p rintf ("%lld", Toten (str)); return 0;}
Two or 10--16
Idea: simple addition to K-take-rest method
Code:
#include <stdio.h>/* to handle up to 50 decimal digit strings, change */#define M 50int Main () {int I=0,j,len,temp;long Num;char str[m] as needed; scanf ("%ld", &num);d o{/* except for the K-remainder */temp = (num%16); */* The number *//* remainder of the 16 binary is determined according to remainders less than 10, plus the character ' 0 ' */if (temp<10) {Str[i] = temp+ ' 0 '; }else{/* remainder greater than 10, plus character ' a ' (can also be small A */str[i] = temp-10 + ' a ';} i++;/* except K rounding */num/=16;} while (num);/* Flashback output array str for each character */for (j=i-1;j>=0;j--) {printf ("%c", Str[j]);} return 0;}
Hexadecimal---decimal conversion