#include<stdio.h> #define MAXLINELEN 1000 int power(int base,int n) { int i=0; int answer=1; for(i=0;i<n;i++) answer=answer*base; return answer; } /*strlen函数:返回s的长度*/ int strlen(char *s) { int i; i=0; while(s[i]!='\0') ++i; return i; } /*getline函数*/ void getlines(char *s) { int i=0; int lim=MAXLINELEN; int c; for(i=0;i<lim-1&&(c=getchar())!='\n'&&c!=EOF;i++) s[i]=c; } /*hexalpha_to_int函数:把单个char转换成int*/ int hexalpha_to_int(int c) { if(c>='0'&&c<='9') c=c-'0'; else if(c>='a'&&c<='z') c=c-'a'+10; else if(c>='A'&&c<='Z') c=c-'A'+10; return c; } /**/ int htoi(char s[]) { int len; int i; len=strlen(s); int answer=0; if(s[0]=='0'&&(s[1]=='X'||s[1]=='x')) { for(i=2;i<len;i++) { answer=hexalpha_to_int(s[i])*power(16,len-1-i)+answer; } } else { for(i=0;i<len;i++) { answer=hexalpha_to_int(s[i])*power(16,len-1-i)+answer; printf("i:%d\n",i); printf("hexalpha_to_int(s[i]):%d\n",hexalpha_to_int(s[i])); printf("power(16,len-1-i):%d\n",power(16,len-1-i)); printf("answer:%d\n",answer); } } return answer; } main() { char s[MAXLINELEN]; getlines(s); printf("%d\n",htoi(s)); }