在乌班图20 的版本上
时间: 2023-05-23 07:01:10 浏览: 99
下面是C语言编程的十六进制转十进制代码:
```c
#include <stdio.h>
int main() {
char hex[20];
int decimal, place;
printf("Enter any hexadecimal number: ");
scanf("%s", hex);
decimal = 0, place = 1;
for (int i = strlen(hex) - 1; i >= 0; i--) {
int hexDigit;
if (hex[i] >= '0' && hex[i] <= '9') {
hexDigit = hex[i] - '0';
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
hexDigit = hex[i] - 'a' + 10;
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
hexDigit = hex[i] - 'A' + 10;
} else {
printf("Invalid hex input!\n");
return 0;
}
decimal += hexDigit * place;
place *= 16;
}
printf("The decimal value is: %d\n", decimal);
}
```
阅读全文