#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int toAnyBase(int n, int toBase); // from base 10 (2nd)
int toBaseTen(char array[256], int fromBase); // from any base (1st)
int fromAnyToAny(char input[],int fromBase,int toBase);

int main()
{
    char input[10];
    int fromBase=0;
    int toBase=0;
    int answer=0;
    int answer2=0;
    

    printf("Enter number :");
    fgets(input, sizeof(input),stdin);
    printf("\n Enter base of input: ");
    scanf("%d",&fromBase);
    printf("\n Enter base of output: ");
    scanf("%d",&toBase);
    answer=fromAnyToAny(input,fromBase,toBase);
    system("pause");
    return 0;
}


int fromAnyToAny(char input[],int fromBase,int toBase)
{
    int x=0;
    int y=0;
    x=toBaseTen(input,fromBase);
    y=toAnyBase(x,toBase);
    return y;
    printf("%d", y);
}

int toAnyBase(int n, int toBase) 
{ 
    int counter=0;
    int bin[256];
    int quo=1;
    int answer;
    
    
    while(quo!=0)
        {
            quo=n/toBase;
            bin[counter]= n % toBase;
            counter++;
            n=quo;
        }
        
    while(counter>0)
        {
            printf("%d",bin[counter-1]);
            counter--;
        }
        printf("\n");
return answer;
}
    
    
int toBaseTen(char array[256], int fromBase)
{
    int i,j;
    int arraylen;
    int n;
    int sum=0;
    int pwr;
        
    arraylen=strlen(array)- 1;
         for(i=0,j=arraylen-1;i<arraylen,j>-1;i++,j--)
            {          
              n = (array[i] - '0');
              sum+= n*pow(fromBase,j);
            }
    return sum;
}

I am having trouble converting from a base10 number to any other base between 2 and 10. For example if i convert 255 from base 10 to base 2 i am getting 11111110 instead of 11111111. Can someone please help?

>For example if i convert 255 from base 10 to base 2
>i am getting 11111110 instead of 11111111.
That particular example works fine for me. Though your code has some...interesting aspects.

Well i am new to C programming. When i use the tweo functions "int toBaseTen(char array[256], int fromBase)" and "int toAnyBase(int n, int toBase)" as two separate programs i am getting the desired output. But its not working when its as one unit. If i try converting from any base (betweeen 2-10) to base 10 it works fine. Example 513 from base 7 to base 10 is 255.

So...when doesn't it work? So far you've given me one example that doesn't work for you but does work for me, and one example that works for you. An example that doesn't work for both of us would expedite the troubleshooting process just a smidge.

Ok. its working but giving errenuous results.
Example: If i convert a base 10 number (255) to base 2 i get 11111110;
When i convert the result 11111110 back to base 10 i get 254.

i should get 11111111 from the first conversion and not 11111110

>Example: If i convert a base 10 number (255) to base 2 i get 11111110;
I repeat. My testing does not show this behavior. If I run your program (exactly as posted), type 255 for the value, 10 for the input base, and 2 for the output base, the result is printed as 11111111. Your example is faulty, or your description of how to reproduce the program is faulty.

>Example: If i convert a base 10 number (255) to base 2 i get 11111110;
I repeat. My testing does not show this behavior. If I run your program (exactly as posted), type 255 for the value, 10 for the input base, and 2 for the output base, the result is printed as 11111111. Your example is faulty, or your description of how to reproduce the program is faulty.

I did the same thing that you did on more than 1 computer and i get the same problem. Lets say u wanna convert 255 from base 10 to base 10. Youre supposed to get 255 as the answer, howeever i am getting 254. when i try to convert any number from 100 up from base ten to any other base i am getting the answer as 1 short the correct answer.

I did the same thing that you did on more than 1 computer and i get the same problem. Lets say u wanna convert 255 from base 10 to base 10. Youre supposed to get 255 as the answer, howeever i am getting 254. when i try to convert any number from 100 up from base ten to any other base i am getting the answer as 1 short the correct answer.

No such problem here. I have to agree with Narue, your code works (more or less) for me. There are a few other thing wrong with it though: Always initialize your vars with a value! Always check if your input is valid! etc

What compiler and OS are you using? I tested it with VS2005 on WinXP

I did the same thing that you did on more than 1 computer and i get the same problem. Lets say u wanna convert 255 from base 10 to base 10. Youre supposed to get 255 as the answer, howeever i am getting 254. when i try to convert any number from 100 up from base ten to any other base i am getting the answer as 1 short the correct answer.

The problem is in the way your compiler deals with this particular statement:

sum+= n*pow(fromBase,j);

You are loosing value. sum is an integer, but the result of the multiplication of n and pow() is a float. The conversion to integer looses some value.
A couple of simple printfs will show the case:

int toBaseTen(char array[256], int fromBase)
{
    int i,j;
    int arraylen;
    int n;
    int sum=0;
         
    arraylen=strlen(array)- 1;
    
         for(i=0,j=arraylen-1;i<arraylen,j>-1;i++,j--)
         {
	          n = (array[i] - '0');
              printf( "n = %d;  n*pow( fromBase, j ) = %.2f\n", n, ( n*pow(fromBase, j)) );
              sum+= n*pow(fromBase,j);
              printf( "sum = %d\n", sum );
              
         }
    return sum;
}
/* input/output:
Enter number :255
Enter base of input: 10
Enter base of output: 2

n = 2 n*pow( fromBase, j ) = 200.00
sum = 199
n = 5 n*pow( fromBase, j ) = 50.00
sum = 249
n = 5 n*pow( fromBase, j ) = 5.00
sum = 254
11111110
Press any key to continue . . .
*/

As you see the first sum should have shown a value of 200 as expected, however it holds only a 199 value carrying over all the way down a deficit of 1.

Thanks Aia...i realize if i declare sum as of type double instead of integer i get the correct value.
Thanks everybody

Thanks Aia...i realize if i declare sum as of type double instead of integer i get the correct value.
Thanks everybody

For further understanding about converting a floating point to an integer take a look at this reading. Particularly to the differences between truncating conversion and rounding conversion.
Perhaps this one and this one will show also some light to the matter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.