user-0 提问: 十六进制转化为十进制问题，程序哪里出错了

回复:
user: user-1
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-2
content: 楼主，你的这个语句if(s[j]>=0&&s[j]<=9)应该改为if(s[j]>='0'&&s[j]<='9')  ，即0和9改为字符型

user: user-1
content: 仅供参考：#include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h> int main() {     int i,v;     char bs[33];     char b[33];     char hs[9];     char h[9];     char s[4];     char *e;  // 十进制整数转二进制串；     i=1024;     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("i=%d,bs=%s\n",i,bs); // 十进制整数转十六进制串；     i=1024;     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("i=%d,hs=%s\n",i,hs); // 十六进制字符串转成十进制数     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     printf("hs=%s,i=%d\n",hs,i); // 二进制字符串转化为十六进制字符串；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("bs=%s,hs=%s\n",bs,hs); // 二进制字符串转化为十进制数；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     printf("bs=%s,i=%d\n",bs,i); // 十六进制字符串转成二进制串     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("hs=%s,bs=%s\n",hs,bs); // ASC\GBK字符串转十六进制串     strcpy(s,"a汉");     i=0;     while (1) {         if (0==s[i]) break;         sprintf(hs+i*2,"%02X",(unsigned char)s[i]);         i++;     }     setlocale(LC_ALL,"chs");     printf("s=%s,hs=%s\n",s,hs); // 十六进制字符串转成汉字（GBK）及字符(ASC)     strcpy(hs,"61BABA");     i=0;     while (1) {         if (1!=sscanf(hs+i*2,"%2x",&v)) break;         s[i]=(char)v;         i++;     }     s[i]=0;     printf("hs=%s,s=%s\n",hs,s);      return 0;  } //i=1024,bs=00000000000000000000010000000000 //i=1024,hs=00000400 //hs=00000400,i=1024 //bs=00000000000000000000010000000000,hs=00000400 //bs=00000000000000000000010000000000,i=1024 //hs=00000400,bs=00000000000000000000010000000000 //s=a汉,hs=61BABA //hs=61BABA,s=a汉

user: user-3
content: 修改如下：  #include<stdio.h> #define YES 1 #define NO 0 #define MAX 10 int htoi(char *s); int main() { 	int c, t; 	char s[MAX + 1]; 	int i = 0; 	while (i < MAX && (c = getchar()) != '\n') 		s[i++] = c; 	s[i] = '\0'; 	t = htoi(s); 	printf("%s=%d\n", s, t); 	return 0; } /*htoi:convert hexadecimal strings to integer*/ int htoi(char *s) { 	int hexdigit, j, inhex, n; 	j = 0; 	if (s[j] == '0') { 		++j; 		if (s[j] == 'x' || s[j] == 'X') { 			++j; 		} 	} 	n = 0; 	inhex = YES; 	for (; inhex == YES; ++j) { 		if (s[j] >= '0' && s[j] <= '9') 			hexdigit = s[j] - '0'; 		else if (s[j] >= 'a' && s[j] <= 'f') 			hexdigit = s[j] - 'a' + 10; 		else if (s[j] >= 'A' && s[j] <= 'F') 			hexdigit = s[j] - 'A' + 10; 		else 			inhex = NO; 		if (inhex == YES) 			n = 16 * n + hexdigit; 	} 	return n; } //0x123 //0x123=291

user: user-4
content: # include <stdio.h>  int htoi (char a []) {     int r = 0, tr, fr = 1, cnt = 0;      while (a [cnt] != '\0') cnt ++;      while (cnt --)     {         if (a [cnt] >= '0' && a [cnt] <= '9') tr = a [cnt] - '0';         else switch (a [cnt])         {             case 'a': case 'A': tr = 10; break;             case 'b': case 'B': tr = 11; break;             case 'c': case 'C': tr = 12; break;             case 'd': case 'D': tr = 13; break;             case 'e': case 'E': tr = 14; break;             case 'f': case 'F': tr = 15; break;             default: return -1;         }         r += tr * fr, fr *= 16;     }      return r; }  int main (void) {     printf ("%d\n", htoi ("face")); }

user: user-5
content: 都是字符比较，按照以下的改过来就好了：  if(s[j] >= '0' && s[j] <= '9')