我正在做C编程语言书中的练习,练习2-3要求我们编写一个函数htoi,将十六进制数转换为十进制数。
这是我编写的代码,但是当它运行时,它总是显示我的十六进制数字是非法的。
请帮帮我!
#include<stdio.h>
#define TRUE 1
#define FALSE 0
int htoi (char s[]);
int main() {
printf("The decimal number is %d\n", htoi("0x134"));
return 0;
}
int htoi (char s[]) {
int j; /* counter for the string */
int temp; /* temp number in between conversion */
int number; /* the converted number */
int ishex; /* if the number is a valid hexadecimal number */
char c;
number = 0;
temp = 0;
ishex = FALSE;
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
ishex = TRUE;
}
else {
ishex = FALSE;
printf("This is not valid hexadecimal number.\n");
return number = 0;
}
if (ishex == TRUE) {
for (j = 2; (c = s[j]) != EOF; ++j) {
if (c >= '0' && c <= '9')
temp = c - '0';
else if (c >= 'a' && c <= 'f')
temp = 10 + c - 'a';
else if (c >= 'A' && c <= 'F')
temp = 10 + c - 'A';
else {
printf("This is a illegal hexadecimal number.\n");
ishex = FALSE;
return 0;
}
number = number * 16 + temp;
}
}
return number;
}发布于 2015-11-18 05:22:53
字符串是在第一个'\0'字符处终止的一系列字符。这意味着"0x134"终止时使用'\0'字符值,而不是EOF值。
您正在对期望由EOF值终止的一系列字符进行操作,但这是不可能的。我稍后会解释为什么..。就目前而言,字符串"0x134"不包含EOF值。
您的循环到达字符串终止的'\0',它不在0..9、a..f或A..F的范围内,因此这个分支执行:
else {
printf("This is a illegal hexadecimal number.\n");
ishex = FALSE;
return 0;
}也许你打算这样写你的循环:
for (j = 2; (c = s[j]) != '\0'; ++j) {
/* SNIP */
}我承诺解释期望EOF作为字符值存在的错误之处。假设一个unsigned char是8位,getchar可以返回256个字符值中的一个,并将它们作为一个正的unsigned char值返回。或它可以返回负的int值EOF,对应于错误或文件结束.
迷惑了?在一个空文件中,没有字符..。但是,如果尝试从文件中读取字符,则每次都会得到EOF,尽管没有字符。因此,EOF不是一个字符值。它是一个int值,在尝试将该值转换为一个字符之前,应该将其视为此值,如下所示:
int c = getchar();
if (c == EOF) {
/* Here, c is NOT A CHARACTER VALUE! *
* It's more like an error code ... *
* XXX: Break or return or something */
}
else {
/* Here, c IS a character value, ... *
* so the following conversion is ok */
char ch = c;
}另外,当c是范围内的数字之一时,c >= '0' && c <= '9'将真实地计算0..9.这是C标准的要求
然而,c >= 'a' && c <= 'f'和c >= 'A' && c <= 'F'在任何情况下都不需要真实地进行评估。它恰好在您的系统上工作,因为您使用的是ASCII,它在一个连续块中包含所有小写字母,在另一个连续块中包含所有大写字母。C不要求ASCII是字符集。
如果希望该代码可移植地工作,可以考虑如下所示:
char alpha_digit[] = "aAbBcCdDeEfF";
if (c >= '0' && c <= '9') {
c -= '0';
}
else if (strchr(alpha_digit, c)) {
c = 10 + (strchr(alpha_digit, c) - alpha_digit) / 2;
}
else {
/* SNIP... XXX invalid digit */
}https://stackoverflow.com/questions/33685190
复制相似问题