【发布时间】:2014-09-06 21:22:47
【问题描述】:
我是 C 的新手,我正在研究一种将 ascii 转换为整数的方法。因此,基本上,如果我有 ABCD(以 16 为基数),我将得到 43981(以 10 为基数).. 只需几步了解我所拥有的。我每次从字符串中取一个数字,然后需要翻译那个数字,所以我调用我的 chartoint 方法。然后我认为在添加新数字之前,我需要 * 基数的先前结果。我也对 printf 方法感到困惑。到目前为止,这是我的方法。
void ascii2int(char *ascii, int base){
int totalstring = 0;
if(2<= base && base <= 16){
for (int i = 0; i < strlen(ascii); i++) {
// Extract a character
char c = ascii[i];
// Run the character through char2int
int digit = char2int(c);
totalstring= digit * base;
printf("%d/n",totalstring);
}
}
}
char2int
int char2int(char digit){
int converted = 0;
if(digit >= '0' && digit <= '9'){
converted = digit - '0';
}
else if( digit >= 'A' && digit <= 'F'){
converted = digit - 'A' + 10;
}
else{
converted = -1;}
return converted;
}
【问题讨论】: