# Thread: Having problem with base converting program

1. Member Join Date
Jan 2015
Posts
2
Rep Power
0

## Having problem with base converting program

Before I begin, I need to confess that this is indeed, for an assignment for class. However, I have worked quote hard on it, only to hit a brick wall.
The idea behind this program is that the program prompts the user to input an initial base (2-36), which checks to ensure that it is a valid int, then asks for a number to convert (which is taken as a String), then it asks which desired base the user would like to convert said number to. I have a basic program that is not complete, but allows me to do a few conversions using convertTo. I believe that I am going to have abandon this method and try mathematically converting every number. This leads me to what I can and cannot do. I am unable to use the initialBase as a condition to know what kind of number i an converting. For instance, I don't know how to make program know that if "2" is the initialBase, that that means that the String is a binary number. THAT is what I'm having problems with.
Here is my initial program that has a few things that are copied and pasted from other bits of my code in my program:
XML Code:
`http://pastebin.com/6uu9pq7S`
I have broken down what I (think I) need to do here:
1. Check to see if the input base is 2, 8, 10, 16, or 32. Hint: Put the possible bases in an array, and check the input base against the array.
2. Check to see if the input number is valid for the base. Hint: Create a String "0123456789ABC...V" and compare each input character with the first "base" characters of the String.
3. Check to see if the output base is 2, 8, 10, 16, or 32. Hint: Use the same possible bases array you used in step 1 to verify the input base.
4. Check to see if the input base is equal to the output base. If so, print the input number.
5. Perform a conversion from the input base to base 10.
6. Perform a conversion from base 10 to the output base. You do this in 2 steps because it's easier to check each conversion separately.
7. Output the converted number.  Reply With Quote

2. ## Re: Having problem with base converting program

What have you tried?

if "2" is the initialBase, that that means that the String is a binary number. THAT is what I'm having problems with.
I don't understand what that problem is. What is the problem about base 2 being binary or base 10 being decimal or base 16 being hexadecimal? There are probably names for lots of different bases.  Reply With Quote

3. Member Join Date
Jan 2015
Posts
2
Rep Power
0

## Re: Having problem with base converting program

Here is an example of what I need"
"Enter a number to convert: "
*user enters number
"Enter what the base is of this number: "
*user enters what base format the number is*
"Now enter what base you'd like to convert this number to: "
*user enters what base to convert number to*
(conversion takes place behind the scenes)
Converted number is printed

What I am having problems with is how to make the program aware what base I am using initially. I can create a program where you enter a specific number in a require base (like hex) and have it convert it into say binary, but I don't know how to let the program know that something is initially a certain base and it know how to convert it to whatever provided finalBase is required.  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Having problem with base converting program Originally Posted by Gaboo What I am having problems with is how to make the program aware what base I am using initially. I can create a program where you enter a specific number in a require base (like hex) and have it convert it into say binary, but I don't know how to let the program know that something is initially a certain base and it know how to convert it to whatever provided finalBase is required.
Well you prompt for the input base and then the number, and then the output base. So let's assume you want to convert hex to binary.

prompt for input base: 16
input number: C7 (you also need to check that it conforms to base).
output base: 2
output number would be 11000111

Regards,
Jim  Reply With Quote

5. ## Re: Having problem with base converting program

Basically, you need to convert a String to an int (given a certain radix) and you need to convert from an int to a String (given (another) radix). If you setup your code like this:

Java Code:
```int convertFrom(String input, int radix) { ... }
String convertTo(int number, int radix) { ... }```
... you're halfway there ;-)

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•