1
  • Parameters: number -- the number you are converting a string
  • base -- the base you are converting the numebr to
    • Return: The number as a string in base "base"
    • Return Type: char *

This is my code but it is not working;

char *toBase(int number, int base) { 
   char *result=malloc(80); 
   char *ans=malloc(80); 
   int i=0,j,c; 
   while(number != 0) { 
      result[i] = number % base; 
      number = number / base; ++i;
   } 
   for(j=i-1;j>=0;j--) {
      if(result[j]>=10 && result[j]<=24) {
           printf("%c",(result[j]+55)); 
           *ans++=(result[j]+55);
      } else {
           printf("%d",(result[j])); 
           *ans++=(result[j]);
      } 
   } 
   return ans; 
}
Soren
  • 14,402
  • 4
  • 41
  • 67
max
  • 167
  • 2
  • 9
  • I added printf statements to check whether it was working or not. The printf statements are giving me the correct answer but I am getting nothing when the function is called. – max Nov 04 '15 at 06:36
  • 1
    When you add a digit, you should say `*ans++ = result[j] + '0';` to get the character representation of a number. As is, you are adding characters with codes 0 to 9, which are mostly unprintable control characters. (It is probably a good idea to move finding the right digit representation to a separate function.) – M Oehm Nov 04 '15 at 06:40
  • You should also null-terminate `ans` after the last loop. – M Oehm Nov 04 '15 at 06:43
  • You can do this with a single buffer. After your `while` loop, reverse the buffer in-place. – keithmo Nov 04 '15 at 06:54
  • Your algorithm seems to be wrong, or at least very complicated. Please check also if base is > 0! If you have a number 101001 with the base 2. You convert it into 10 base by starting with the last digit on the right side: 1 * 2^0 + 0 * 2^1 + 0 * 2^2 + 1 * 2^3 + 0 * 2^4 + 1 * 2^5 or in general with any other base : digit1 * base^0 + digit2 * base^1 + digit3 * base^2 + digit4 * base^3 First I would convert your number into an array convert digits into array: http://stackoverflow.com/a/515660/5470883 this example uses a 10 base number but it works also for other bases by using log(a) / log(base) – Alexander Baltasar Nov 04 '15 at 07:37

2 Answers2

2

The problem is that you modify the ans pointer in the loop, so when you return ans it no longer points to the beginning of the string, instead it points to where you should terminate the string.

Use a temporary variable initialized to be the same as ans and return the one you don't modify.

It should also be noted that you have a memory leak, you allocate memory for result but you never free it anywhere.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I got it by using a temporary variable initialized to be the same as ans. Thanks you so much. – max Nov 04 '15 at 06:59
1

Your problem is in the conversion of the numbers into text

Try to add the value of '0' and 'A' resectively, as

  if(result[j]>=10 && result[j]<=24) {
       printf("%c",(result[j]+'A')); 
       *ans++=(result[j]+'A');
  } else {
       printf("%c",(result[j]+'0')); 
       *ans++=(result[j]+'0');
  }

and make sure to null terminate the string before returning it, like

*ans++ = 0;

and also -- the statement

return ans;

will return the end of the string rather than the start -- so you will have to fix that as well, and of cause fix your memory leaks....

Soren
  • 14,402
  • 4
  • 41
  • 67
  • I am getting my answer by not adding 'A' or '0'. – max Nov 04 '15 at 07:35
  • Sure -- you are adding 55 which is the same as '7' -- which makes a very strange toBase function, but maybe that is your requirement for the function. You are not saving anything by not using proper symbolic values like '0' and 'A', they are just byte values (not strings) with values of 48 and 65 -- but they make your code a lot more readable for the next guy looking at your code. – Soren Nov 04 '15 at 07:41
  • Ohh, thank you for your suggestion. I will keep that in mind – max Nov 04 '15 at 07:49