# Thread: converting to base 10

1. ## converting to base 10

Hi,

I'm currently writing a program that takes a number (n) and a base (b) between [1, 10] from the user's input and convert it into base 10. However, I have to make sure that all the digits of n has to be between [0, b-1] with the exception of n being 1. For example:

n = 012 and b =3, output is 5
n = 00110 and b=2, output is 6
n = 00110 and b=1, output is 2

for base (b) = 1, the current program cannot check to see if the digits of 'n' is greater than 1 or not because of this code that i use to check it: output[index] = n%b. For example:

n=23 and b = 2 : output[index] = 23%2 = 3 ; and 3 is greater than base 2
BUT if n = 23 and b =1; ouput[index] = 23%1 = 0; and I CANNOT TAKE THE 3 to compare to base 1 so that 3 is greater than 1.

Code:
```#include<stdio.h>

int main(void)
{
int n, b;
int output[64];
int index = 0;

printf("Enter base(b) between [1,10], and a number(n) between [0,b-1] in this format 'n b': ");
scanf("%d %d", &n, &b);

printf("n is: %d\n", n);   //Test
printf("b is: %d\n", b);   //TEST

if ((b < 1) || (b > 10))
{
printf("Your base is not between 1 and 10");
return 0;
}

if (n < 0)
{
printf("n must be positive");
return 0;
}

while (n != 0)
{
output[index] = n % b;

if ((output[index] >= b) && (output[index] != 1))  //TROUBLE AREA RIGHT HERE
{
printf("one of the digits is greater than the base");
return 0;
}

n = n/b;
++index;
printf("number to convert: %d\n", n);
}
return 0;
}```
Thank you!

2. Well the if() inside the while loop makes no sense, since it can never be true. You've just done n&#37;b, so output[index] >= b will always be false.

Also, what is the other printf() supposed to do?
Printing output[index] would tell you more IMO.

3. Sorry, the while loop is supposed to look like this:
Code:
```#include<stdio.h>

int main(void)
{
int n, b;
int output[64];
int index = 0;

printf("Enter base(b) between [1,10], and a number(n) between [0,b-1] in this format 'n b': ");
scanf("&#37;d %d", &n, &b);

printf("n is: %d\n", n);   //Test
printf("b is: %d\n", b);   //TEST

if ((b < 1) || (b > 10))
{
printf("Your base is not between 1 and 10");
return 0;
}

if (n < 0)
{
printf("n must be positive");
return 0;
}

while (n != 0)
{
output[index] = n % 10;

if ((output[index] >= b) && (output[index] != 1))  //TROUBLE AREA RIGHT HERE
{
printf("one of the digits is greater than the base");
return 0;
}

n = n/b;
++index;
printf("number to convert: %d\n", n);
}
return 0;
}```
For some reason if i put n = 110 and b=2, it goes into the if statement, which it is not supposed to.

4. This problem is WAY easier if you read the "n" value as a string instead of an integer. You are going to be processing it digit-wise, anyway, so why convert from digits to a value only to convert back to digits again?

5. Hi, I'm reading "n" as a string now, but for some reason I'm keep getting a "segmentation fault (core dumped)". Do you know why is it doing this?

Code:
```#include<stdio.h>

int main(void)
{
int b;
char n;
int output[64];
int index = 0;

printf("Enter base(b) between [1,10], and a number(n), such that digits of n is between[0,b-1] in this format 'n b': ");
scanf("&#37;s %d", &n, &b);

printf("n is: %s\n", n);   //Test
printf("b is: %d\n", b);   //TEST
/*
if ((b < 1) || (b > 10))
{
printf("Your base is not between 1 and 10");
return 0;
}

if (n < 0)
{
printf("n must be positive");
return 0;
}

while (n != 0)
{
output[index] = n % 10;
n = n/b;
++index;
printf("number to convert: %d\n", n);
}*/
return 0;
}```
Thank you!

6. You're not reading it as a string, you're reading it as a single char (but with string format).

--
Mats

7. Originally Posted by xp5
Hi, I'm reading "n" as a string now, but for some reason I'm keep getting a "segmentation fault (core dumped)". Do you know why is it doing this?
n needs to be an array large enough to hold the input. E.g:

Code:
`char n[64];`
Also, you'll have to change other parts of the code, because processing digit-by-digit is completely different than your old method. But the result will be much simpler than what you have now.

Since I suspect this is homework, I won't post an actual solution -- but you should be able to solve the entire problem in fewer than 20 lines of code. The function I wrote which converts the number string from its given base is only three lines long.

8. Originally Posted by brewbuck
n needs to be an array large enough to hold the input. E.g:

Code:
`char n[64];`
Also, you'll have to change other parts of the code, because processing digit-by-digit is completely different than your old method. But the result will be much simpler than what you have now.

Since I suspect this is homework, I won't post an actual solution -- but you should be able to solve the entire problem in fewer than 20 lines of code. The function I wrote which converts the number string from its given base is only three lines long.
There's even a library function to do that, right?

By the way, is base 1 actually a valid base? What's the semantics of "base 1"?

--
Mats

9. Originally Posted by matsp
By the way, is base 1 actually a valid base? What's the semantics of "base 1"?
Tick marks.

1 = 1
2 = 11
3 = 111
4 = 1111
5 = 11111
etc...

EDIT: Base 1 was probably the first method of written counting ever invented. Even more amusing is the concept of "Base 0," where each number gets its own unique symbol:

1 = Harry
2 = Sam
3 = Bobby
4 = Johnny
5 = Terrence
etc...

10. For some reason if i put n = 110 and b=2, it goes into the if statement, which it is not supposed to.
Thats becuase of this

Code:
`n = n/b;  /* Which should have been n/10`
But any way you should ry this problem with the solution, using string. What other sugguested you.

HINT: The thing which u are trying to do can be done using strtol function.
ssharish2005

11. Actually, I can't use char in this problem, everything has to be integer. But, I got it now! Thanks!!

12. Originally Posted by xp5
Actually, I can't use char in this problem, everything has to be integer. But, I got it now! Thanks!!
Consider that you may want to input base > 10, in which case the normal numbers don't suffice, so you you need to read the original number in as a string - not a single char of course, but an array of chars (which, when it's terminated with a zero(0 non '0') is a string in C).

--
Mats