/*
請輸入一個16進位制數 : FF
這個數的10進位制是 : 255
Press any key to continue
*/
#include <stdio.h>
unsigned Hex2Dec(char hex[]) {
unsigned i,num = 0;
for(i = 0; hex[i]; ++i) {
if(hex[i] >= "a" && hex[i] <= "f")
num = 16 * num + hex[i] - "a" + 10;
else if(hex[i] >= "A" && hex[i] <= "F")
num = 16 * num + hex[i] - "A" + 10;
else if(hex[i] >= "0" && hex[i] <= "9")
num = 16 * num + hex[i] - "0";
}
return num;
int main() {
char s[9];
printf("請輸入一個16進位制數 : ");
scanf("%s",s);
printf("這個數的10進位制是 : %u\n",Hex2Dec(s));
return 0;
/*
請輸入一個16進位制數 : FF
這個數的10進位制是 : 255
Press any key to continue
*/
#include <stdio.h>
unsigned Hex2Dec(char hex[]) {
unsigned i,num = 0;
for(i = 0; hex[i]; ++i) {
if(hex[i] >= "a" && hex[i] <= "f")
num = 16 * num + hex[i] - "a" + 10;
else if(hex[i] >= "A" && hex[i] <= "F")
num = 16 * num + hex[i] - "A" + 10;
else if(hex[i] >= "0" && hex[i] <= "9")
num = 16 * num + hex[i] - "0";
}
return num;
}
int main() {
char s[9];
printf("請輸入一個16進位制數 : ");
scanf("%s",s);
printf("這個數的10進位制是 : %u\n",Hex2Dec(s));
return 0;
}