# algorithm to code.

This is a discussion on algorithm to code. within the C Programming forums, part of the General Programming Boards category; hey...I got this algorithm of conversion and now I'm stuck at how to code it. Can anyone please help. "Algorithm ...

1. ## algorithm to code.

hey...I got this algorithm of conversion and now I'm stuck at how to code it. Can anyone please help.

"Algorithm to Convert From any Base to Base 10 Decimal.

Let 'n' be the number of digits in the number. For example, 104 has 3 digits, so 'n'=3.
Let 'b' be the base of the number. For example, 104 is decimal so 'b' = 10.
Let 's' be a running total, initially 0.
For each digit in the number, working left to right do:
Subtract 1 from 'n'.
Multiply the digit times b^n and add it to 's'.
When done with all the digits in the number, the decimal value should be 's' .

2. That's exactly what the number line means.

But that won't help you start your program, I guess. Run through the C tutorial (click the tab for it at the top of this page), and you'll know enough to get at least a minimal start.

The start of a program is almost always 100% up to YOU, on the programming forums - you can (and must) be able to do that, at least.

3. I did start the coding but I'm stuck with that part.

4. I did start the coding but I'm stuck with that part.
Really? Maybe it would help if you showed what you have.

Jim

5. You'll get very confused if you talk about number bases in C. Internally all numbers are stored as binary patterns of bits. Then in C source numbers are decimal by default, of hexadecimal or octal if given the right prefixes.
But C programs can handle numbers in any base, represented as ascii strings or, if you want a huge base, arrays of integers. Not atomic operations will work on such numbers, you have to write your own functions to add, subtract, print them out, etc.
Normally no-one does that, and everything is converted to the internal binary representation as soon as possible.

However mathematically there's nothing special about base ten. It's certainly possible to write a function to convert from an arbitrary base to another arbitrary base.

6. there is the code...I only get wrong values when running it.

Code:
```int main()
{

char s[20];
int base;
int r,index,n,p,b=1,sum=0; /* n is the number of digits in the value to be converted */

printf("enter the number and base: ");
scanf("%s %d",s,&base);

for(n=strlen(s)-1;n>=0;n--)
{
for(index=1; index<=strlen(s); index++)
sum += s[index] * pow(base, n);
}
printf("the number in base ten is %d",sum);
printf("\n");
system("pause");
}```

7. Here is a function I wrote to essentially do the opposite of what you are wanting.

Code:
```void itob( int n , char * string , int b ){
int i;
for( i = 0;n > 0; ++i ){
/* nums 0-9 have their respective ascii values inserted into string.
* higher nums have uppercase letters, starting with 'A' for num 10 */

string[ i ] = n % b + ( ( n % b <= 9 ) ? '0' : '7' );

/* adding '0' to the number turns it from a number 0-9 into the ascii version
adding '7' turns the number into the alphabetical equivilant
*/
n /= b;
}
string[ i ] = '\0';
reverse( string );   // reverse() reverse's the order of the string.... durr
return;
}```