0

I'm trying to convert from some base (2-9) to base 10. The way this has to be done involves a lot of conversion between strings and integers because the function has to spit out a string, but it takes three arguments: the number to convert, the starting base, and the end base.

Anyways, here are some of the results I get when trying to convert various numbers from base 2 to base 10:

Number Result
11     2
100    4
101    4
110    4
111    4
1000   8
1001   8
1010   8
1011   8
1100   8

As you can see, a bit of a pattern has emerged. It's only converting the first digit to base ten and ignoring the rest. I'm not really sure why this is happening. Here is the code that is doing it:

char* baseConversion(int number, int inBase, int toBase)
{
    char tempString[20];

    if(number==0)
    {
        tempString[0] = 0;
        return tempString;
    }

    if(toBase == 10)
    {
        sprintf(tempString, "%d", number);
        char tempString2[] = {tempString[0]};
        int tempNumber = (atoi(tempString2)*((int)pow((float)inBase,(float)strlen(tempString)-1.0))+atoi(baseConversion(atoi(tempString+1), inBase, 10)));
        sprintf(tempString, "%d", tempNumber);
        return tempString;
    }
}

Maybe it's something wrong with my algorithm, but I tried it on paper and it seems to work. Thanks.

  • "because the function has to spit out a string" - why? Because you're intending to make the to-base larger than 10? Even if you are, as long as the intermediate number fits in an int it might make sense to decode it into an int and then re-encode it separately. I'd split your do-everything line out into lots of parts (get one digit, compute base^length-1, get next digit, combine) so you can step through and see which one is going wrong. Also if you're extracting digits from a base-10 number as you are here (albeit a base-10 representation of some other base) you can use div and mod not strings – Rup Oct 23 '14 at 23:00
  • 5
    You are returning the address of a local variable. That's undefined behaviour. – mch Oct 23 '14 at 23:01
  • 1
    `char tempString2[] = {tempString[0]};` : tempString2 does not end with NUL characters. – BLUEPIXY Oct 23 '14 at 23:19
  • @mch I'd like to make it clear that in main I'm saying strcpy(string, baseConversion(number, inBase, toBase) so it should be fine (there's another part of the same function that takes a base 10 and changes it to another base and it works just fine). – Ted Kursevicius Oct 23 '14 at 23:20
  • @BLUEPIXY How do I make it end in a null character? I tried char tempString2[] = {tempString[0], 0}; but that didn't work either. – Ted Kursevicius Oct 23 '14 at 23:22
  • 1
    Even apart from @mch's comment, when the number is 0 you are returning an empty string, not "0". The way you have defined the problem makes no sense. A number on a computer is stored as base 2. It would only make sense if you passed a string representation of the number in the base you are starting with. – Weather Vane Oct 23 '14 at 23:22
  • It is preferable to rewrite rather than where is bad. – BLUEPIXY Oct 23 '14 at 23:22
  • @Rup The reason it has to return a string is that this is part of an assignment and our teacher is forcing us to write this function like so. There's no flexibility in that regard. – Ted Kursevicius Oct 23 '14 at 23:28
  • http://ideone.com/CtDABa – BLUEPIXY Oct 23 '14 at 23:52
  • @TedKursevicius try `static char tempString[20];` and `tempString[0] = 0;` --> `tempString[0] = '0';tempString[1] = 0;//NUL` – BLUEPIXY Oct 23 '14 at 23:55
  • Well I still think that using strings for your intermediate state is asking for trouble. If you're allowed more than one function I strongly suggest you write a `readBase` function that accepts the input format and returns an int, and then your `convertBase` just has to pass the result of that into a `writeBase` function to generate a string to output. Both of those can be recursive if that what you need to do too. But at this point you just need to debug: either use an IDE or debugger and step through the program checking state, or put in lots of printfs that dump it out as it goes. – Rup Oct 24 '14 at 00:02
  • @BLUEPIXY All I had to do was add static in front of char tempString[20]; Thank you so much! – Ted Kursevicius Oct 24 '14 at 00:25
  • Note that a static area is rewritten in the recursive calls. – BLUEPIXY Oct 24 '14 at 00:53
  • @BLUEPIXY Oh yeah, that poses a problem for the other parts of my function. I mean, it's easy to hack it, but I feel like I still have to figure out why it's messing up so I can change it properly. – Ted Kursevicius Oct 24 '14 at 01:03

0 Answers0