2 <= base <= 16 converter.
Author |
Message |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Sun Feb 26, 2006 12:44 pm Post subject: 2 <= base <= 16 converter. |
|
|
My apologies for being a noob at math. I dont know how to directly convert bases so I go from currentBase to 10 to desiredBase. The code is straight for ward.
The input file is attached. Its format is:
Number numsBase theBaseYouWantIn
Just changing the variable numberOfInputs would make it read how many linesyou want it to.
code: | import java.io.*;
import java.util.*;
public class Problem5
{
public static void main (String [] Args)
{
try {
BufferedReader in = new BufferedReader (new FileReader ("DATA51.txt"));
BufferedWriter out = new BufferedWriter (new FileWriter ("OUT51.txt"));
int numberOfInputs = 5;
String [] line;
String [] Tenplus = new String [6];
for (int i = 10; i < 16; i++) Tenplus[i-10] = i+"";
String alphas = "ABCDEF";
for (int i = 0; i < numberOfInputs; i++) {
if (i!= 0) out.newLine();
line = in.readLine().split(" ");
String currentNum = line[0];
int currentBase = Integer.parseInt(line[1]);
int desiredBase = Integer.parseInt(line[2]);
int base10Total = 0;
int po = currentNum.length()-1;
//NUMBER IS 0
if (currentNum.charAt(0) == '0' && currentNum.length() == 1) {
out.write ("0");
continue;
}
//CONVERTING INTO BASE 10
for (int j = 0; j < currentNum.length(); j++) {
int index = alphas.indexOf(currentNum.charAt(j));
if (index >= 0) {
base10Total += Integer.parseInt(Tenplus[index]) * Math.pow(currentBase, po);
}
else base10Total += (currentNum.charAt(j) - '0') * Math.pow (currentBase, po);
po--;
}
//CONVERTING INTO DESIRED BASE
String ans = "", reverseAns = "";
int rem = 0;
while (true) {
rem = base10Total % desiredBase;
base10Total = (int) Math.round(base10Total/desiredBase);
if (base10Total == 0 && rem == 0) break;
if (rem >= 10) ans += alphas.charAt(rem-10);
else ans += rem;
}
//REVERSING THE STRING OF MODS
for (int j = ans.length()-1; j >= 0; j--) reverseAns += ans.charAt(j);
//FINAL ANSWER
out.write (reverseAns);
}
in.close();
out.close();
}
catch (IOException e){
}
}
}
|
PS: I know tis a noobie submission, but its my first sub. in java =\
Description: |
Put it in the same folder as the program. =) |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
DATA51.txt |
Filesize: |
55 Bytes |
Downloaded: |
269 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
mckhatri1
|
Posted: Wed Jul 05, 2006 8:22 am Post subject: Easier Way |
|
|
Mind if I help simplify:
code: | import java.io.*;
public class Problem5
{
public static void main (String [] Args) throws Exception
{
BufferedReader in = new BufferedReader (new FileReader ("DATA51.txt"));
BufferedWriter out = new BufferedWriter (new FileWriter ("OUT51.txt"));
for (int i = 0; i < 5; i++) {
String[] stuff=in.readLine().split(" ");
int origBase=Integer.parseInt(stuff[1]);
int newBase=Integer.parseInt(stuff[2]);
int numInDec=Integer.parseInt(stuff[0],origBase);
out.println(Integer.toString(numInDec,newBase).toUpperCase());}
out.close();
}
} |
|
|
|
|
|
![](images/spacer.gif) |
Krabjuice
|
Posted: Wed Jul 05, 2006 10:21 am Post subject: (No subject) |
|
|
How about this? Make sure you download both classes.
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
Hex.java |
Filesize: |
3.64 KB |
Downloaded: |
273 Time(s) |
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
BaseChange.java |
Filesize: |
7.15 KB |
Downloaded: |
393 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Wed Jul 05, 2006 11:52 am Post subject: (No subject) |
|
|
Your indentation is all wrong. You're putting starting braces on their own lines. You have spaces between open parens and class names as well as in method calls in places. You have spaces between array names and subscripts. You sometimes use cryptic variable names like "po". You lack proper spacing around some operators.
Now, non-style issues.
code: | if (currentNum.charAt(0) == '0' && currentNum.length() == 1) {
out.write ("0");
continue;
} |
If the string is empty, charAt(0) will throw an exception. You don't catch this, so the end result is that your program will die horribly. If we reverse the two conditions, then short-circuiting saves your bacon.
code: | if (currentNum.length() == 1 && currentNum.charAt(0) == '0') {
out.write ("0");
continue;
} |
Oh, and use methods. This much functionality should not be directly in main.
|
|
|
|
|
![](images/spacer.gif) |
|
|