I'm writing a method that turns a decimal number into any radix (2-36) and prints the appropriate chars to the screen. The decimalToSymbol() method works correctly. The problem I'm having is in the while loop I assign ans[i] to a char and when I try to print it immediately after it is 0. I have looked online and found this is an OK way to assign the char but am not sure what is going wrong.
void writeRadixB(int decimalNumber, int radixB)
{
char ans[80] = "";
int i = 0;
while(decimalNumber!= 0){
printf("decNum: %d div by rB: %d equals %d\n", decimalNumber, radixB, decimalN
printf("decNum: %d mod by rB: %d equals %d or char %c\n", decimalNumber, radix
printf("i: %d\n", i);
decimalNumber = decimalNumber/radixB;
ans[i] = decimalToSymbol(decimalNumber%radixB);
printf("ans[%d] is %c\n", i, ans[i]);
i++;
}
printf("(%s) radix %d", ans, radixB);
}
char ans[80] = {0};you should also checkif (radixB != 0) return;. I'm not quite sure what purpose negative values would have here either. You should also add the (complete) output of your program to enhance the question...