用C语言实现十六进制整形数据类型转换为十进制
时间: 2023-11-16 20:07:17 浏览: 88
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main() {
char hex_num[100];
int dec_num = 0;
printf("Enter a hexadecimal number: ");
scanf("%s", hex_num);
int len = strlen(hex_num);
for (int i = 0; i < len; i++) {
if (isdigit(hex_num[i])) {
dec_num = dec_num * 16 + (hex_num[i] - '0');
} else if (hex_num[i] >= 'a' && hex_num[i] <= 'f') {
dec_num = dec_num * 16 + (hex_num[i] - 'a' + 10);
} else if (hex_num[i] >= 'A' && hex_num[i] <= 'F') {
dec_num = dec_num * 16 + (hex_num[i] - 'A' + 10);
}
}
printf("The decimal equivalent of %s is %d\n", hex_num, dec_num);
return 0;
}
阅读全文