【发布时间】:2014-12-14 05:53:17
【问题描述】:
所以我在创建递归函数以将数字从基数 2-10 转换为基数 2-16 时遇到了麻烦。我需要它返回一个字符串(显然,由于基数大于 10)。
这是我的功能:
main 会这样称呼它:
answer = baseConversion(101, 10, 2);
我将十六进制作为常量字符:
const char Hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char * baseConverter(int number,int currbase, int base){
if(currbase != 10){
number = base10Converter(number, currbase); //converts the number to base of 10
currbase = 10;
}
if(number == 0 || base==10){
return number;
}
int r = number%base;
printf("%c", Hex[r]);
//return (number % base) + 10*baseConverter(number /base, currbase, base); // this gives the answer as an integer.
return Hex[r]+ && baseConverter(number /base, currbase, base) // I dont know what to add here to add the characters together
}
我的 return 语句和递归调用需要帮助。 我是否需要在函数中声明一个字符数组,然后将我从 hex[r] 获得的字符附加到它上面?如果是这样,我该怎么做,因为我无法更改参数
【问题讨论】:
-
函数base10Converter的内容是什么
标签: c string recursion base-conversion