# number base converter

This is a discussion on number base converter within the C Programming forums, part of the General Programming Boards category; hi i found the itoa non standard function to convert numeber from base 10 to other base here a test ...

1. ## number base converter

hi

i found the itoa non standard function to convert numeber from base 10 to other base

here a test code i have written
Code:
```int main() {
char bin[25];
int n;
scanf("%d",&n);
itoa(n, bin, 2);
printf ("binary: %s\n",bin);
return 0;
}```
now.. how can i do conversion from numbers in base other than 10 to base 10?

i hadn'd understood if the atoi function can help me

2. Use strtol().

3. i searched info on strtol()

so if i want to convert a binary number to 10-base
i must have the binary number in bin
and i must have base=2
and i will found in n the 10-base number?
Code:
`n = strtol(bin, NULL, base);`
all right?

but bin could be a int or must be bin[]??

and sorry for my ignorance in C and also english!!

4. bin must be an int for a long. Once you have it as an int, printing the int in whatever base representation you want is another matter.

5. what means "an int for a long"?

this will work? and is correct?

Code:
```int bin,num;

bin=0100101;

num = strtol(bin, NULL, 2);

printf("%d converted in 10-base is: %d", bin, num);```
thanks.

Let's clarify: given an integer value stored an int, you wish to store its representation in some base other than base 10 in a string? If so, then unless these bases are 8 or 16, I believe that you would have to perform the conversion yourself using some arithmetic.

7. strol()?? it converts char string to long integer. can u convert 10011011 in binary to octal system using strol

8. there isn't need that the number that should be converted is an int

the number is obtained from a scanf

what is the type of the bin variable in the row:
num = strtol(bin, NULL, 2);

?

9. Originally Posted by magestrium
strol()?? it converts char string to long integer. can u convert 10011011 in binary to octal system using strol
Numbers are stored internally as binary.

You do not onvert numbers...

What you can do - read string representation of the number in one format and output number as string in another format...

strtol could read string representation of the number in any base from 2 to 36

When you have read number you can print it as octal, dec or hex using printf, or in any other representation using your own function...

10. Originally Posted by didrochi
the number is obtained from a scanf
That would not matter; once it is read into an int, the process will be the same.

Originally Posted by didrochi
what is the type of the bin variable in the row:
num = strtol(bin, NULL, 2);

?
No, strtol() wouldn't work. I misread your original post. I thought that you were trying to use atoi() with base conversion, but you are actually looking to use something along the lines of sprintf(). sprintf() can handle conversion to octal and hexadecimal, but not other bases (besides decimal, of course).

11. i understood almost anything

so how becomes my function that read a number (base other than 10) from scanf
then read (scanf) the number base

and convert it to 10-base??

Code:
```int n, base;
printf ("Enter a number: ");
scanf ("%d",&n);
printf ("enter base of the number inserted: ");
scanf ("%d",&base);

...```
so should i use a function like this?
Code:
```int convert_base_to_10(int base, int x) {
return (x < base) ? x : convert_base_to_10(base, x / 10) * base + (x % 10);
}```
and there is not a "direct" function to conversion like the reverse itoa??

12. scanf ("%d",&n);

this is only good is number is decimal...

so enter the number as string and use strtol afterwards - when you really know the base

13. someone has a link of a source of a base converter from and to any base (2-16) for study how it work

14. It's not actually all that hard.

Code:
```If value is negative multiply by -1.
Clear result string.
Repeat
Compute value modulo base.  Append representation of that value to result string.
Divide value by base (integral division)
Until value is zero
If original value was negative, append a '-' character.
Reverse string.```
For bases 2-36 this works with a simple mapping of digits to character: Digits 0 through 9 map to '0' through '9'. Remainders/digits 10 through 35 map to 'A' through 'Z'.