0

I have some trouble with my code in C. It doesn't convert the number as it should be. This is the euclidean algorithm to transform a given number to another number with a different base. "precision" is the amount of digits after comma.

static char* euclid(float number, int base, int precision)
{
    //create a buffer for holding the resulting string
    char* resultString = createBuffer(number, base, precision);
    int numbTemp;
    int numbDiv;

    float maxExponent  = 0;

    while (number >= pow((float)base, (float)maxExponent)) {
       maxExponent++;
    }

    maxExponent--;

   if (maxExponent < 0) {

       maxExponent = 0;
   }

   while (maxExponent >= 0) {
     numbDiv = number / pow(base, maxExponent);
     *resultString += converIntToChar(numbDiv);
     numbTemp = numbDiv * pow(base, maxExponent);
     number -= numbTemp;
     maxExponent--;
     if(maxExponent < 0 && precision >0) {
         *resultString += ",";
         while(precision > 0) {
             numbDiv = number / pow(base, maxExponent);
             numbTemp = numbDiv * pow(base, maxExponent);
             *resultString += convertIntToChar(numbDiv);
             number = number - numbTemp;
             maxExponent--;
             precision--;
         }
     }
  }
  return resultString;
}

converting number to character:

static char convertIntToChar(int number) {
     if (0 <= number && 10 > number) {
          return number + '0';
     }
     else if (16 > number) {
          return number - 10 + 'A';
     }
     else {
          return '\0';
     }
}

The input for the "number" is always a number to the base 10. I tried it for example with 170 and as result I wanted to have 170 (which is to the base 10) returned as a number to the base 16, which is AA. But what I get as result is �. Could you help me to find the the problem. Besides I use linux. So I use the gcc compiler. Also I am trying to implement Horner's method and I get the same result as above (�) My code for the Horner's method.

static char* horner(float number, int base, int precision) {

    // create a buffer for holding the resulting string.
    char* resultString = createBuffer(number, base, precision);
    int numbDiv = 0;
    int numbTemp = 0;
    int numbConv = 0;

    do {
        numbDiv = number / base;
        numbTemp = numbDiv * base;
        numbConv = number - numbTemp;
        number = numbDiv;
        *resultString += convertIntToChar(numbConv);
    } while (number != 0);

    return resultString;
 }

regards Hagi

Hagi
  • 87
  • 1
  • 1
  • 5
  • When I enter a number with comma, I get again this symbol �. Just an example, when I enter 15741.233 (to the base 10) I get returned 3D7D�3 (to the base 16, with precision 4 (digits after comma)) but it should be 3D7D.3BA5. I don't know why it does that. Does anyone know why? – Hagi May 12 '13 at 16:43
  • I did not use single quote characters. Now, I get 3D7D.3 as result, but there are still 3 digits missing. Does anyone know why? – Hagi May 12 '13 at 16:49

2 Answers2

0

Your issue is with confusion over what is *resultString; you are supposed to be putting the precisions in resultString[idx] where idx is the next index but all you are doing is just adding the values to 0th character because *resultString points to it;

So do this:

char * resultString = createBuffer(number, base, precision); 

is fine;

but

*resultString += converIntToChar(numbDiv);

is nonsensical

Declare an index variable;

char * resultString = createBuffer(number, base, precision); 
size_t idx = 0;

Now later on :

resultString[idx++] = convertIntToChar(numbDiv);

and in your while(); loop:

*resultString += ","; /* ??? */

becomes

 resultString[idx++] = ','; /* note single character quotes */

and similar treatment of other instances of *resultString;

Once the function is about to return (i.e. the resultString is ready) you still need a terminating NULL character:

 resultString[idx++] = 0; 
 return;

This also means that your createBuffer should return memory that accounts for the NULL character of C strings at the end.

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
  • Thank you for your tip. It's working. Could you help me with another problem? For example the number 15741.233(to the base 10) should be returned as 3D7D.3BA5 (to the base 16). I tried that out and I got 3D7D�3 as result. Do you know why my code does that? – Hagi May 12 '13 at 16:35
  • I don't get the � anymore. It was because I did not do a single character quotes.^^ But now I get 3D7D.3 and there are still 3 digits missing. Do you have a clue why? – Hagi May 12 '13 at 16:48
0

*resultString += some char is not doing a concatenation of a character to a string (as you seem to expect), instead it adds the integral value of some char to the first character of resultString.

Bryan Olivier
  • 5,207
  • 2
  • 16
  • 18