//-- BaseConverter
//
import java.awt.*;
import java.applet.*;
public class BaseConverter extends Applet {
final Color myColor = new Color(200,200,200);
Choice c_In,
c_Out;
TextField tf_In,
tf_Out;
public void init(){
c_In = new Choice();
c_Out = new Choice();
for (int i=2;i<=36;i++){
c_In.addItem(""+i);
c_Out.addItem(""+i);
}
c_In.select(8);
c_Out.select(6);
tf_In = new TextField(10);
tf_Out = new TextField(10);
setBackground(myColor);
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new GridLayout(4,2));
Panel p1a = new Panel();
p1a.add(new Label("Base to convert from:"));
p1a.setLayout(new FlowLayout(FlowLayout.LEFT));
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(c_In);
p.add(p1a);
p.add(p1);
Panel p2a = new Panel();
p2a.setLayout(new FlowLayout(FlowLayout.LEFT));
p2a.add(new Label("Number to convert: "));
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(tf_In);
p.add(p2a);
p.add(p2);
Panel p3a = new Panel();
p3a.setLayout(new FlowLayout(FlowLayout.LEFT));
p3a.add(new Label("Base to convert to:"));
Panel p3 = new Panel();
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
p3.add(c_Out);
p.add(p3a);
p.add(p3);
Panel p4a = new Panel();
p4a.setLayout(new FlowLayout(FlowLayout.LEFT));
p4a.add(new Label("Converted Result: "));
Panel p4 = new Panel();
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
p4.add(tf_Out);
p.add(p4a);
p.add(p4);
add("Center",p);
add("South",new Button("Calculate"));
}
public boolean action(Event evt, Object arg){
tf_Out.setText("");
if (arg.equals("Calculate")){
String s = convertToTen(tf_In.getText(),(int)Integer.parseInt(c_In.getSelectedItem()));
double y = Double.valueOf(s).doubleValue();
tf_Out.setText(convertFromTen(y,(int)Integer.parseInt(c_Out.getSelectedItem())));
}
return true;
}
//
//----------------------//----------------------//-------------------
//
public static String convertToTen(String s1, int base){
System.out.println("Received: "+s1+" base: "+base);
String s;
if (s1.indexOf(".") > 0)
s = s1.substring(0,s1.indexOf("."));
else
s = new String(s1);
// Convert higher bases to 10
int []a = new int[s.length()];
for (int i=0;i= 'a' && c <= 'z')
return (int)Integer.parseInt(""+(int)(c-97+10));
if (c >= 'A' && c <= 'Z')
return (int)Integer.parseInt(""+(int)(c-'A'+10));
if (c >= '0' && c <= '9')
return (int)Integer.parseInt(""+(char)c);
return 0;
}
//
//----------------------//----------------------//-------------------
//
public static char intValueOf(int x){
if (x >= 0 && x <= 9)
return (char)('0'+x);
if (x >= 10)
return (char)('a'+(x-10));
return 0;
}
public static String convertFromTen(double num, int toBase){
String s = "";
int i = findLargest(num,toBase);
while (num > 0.0000000001 || i >= 0) {
if (s.length()>20)
num = 0.0;
if (i == -1)
s+=".";
System.out.println("G: "+findNumLargest(num,toBase,i));
s+=""+intValueOf(findNumLargest(num,toBase,i));
num -= Math.pow(toBase,i)*findNumLargest(num,toBase,i);
i--;
}
return s;
}
public static int findLargest(double num, int toBase){
int n=0;
while (Math.pow(toBase,n) <= num)
n++;
return n-1;
}
public static int findNumLargest(double num, int toBase,int t){
int n=1;
while (n*Math.pow(toBase,t) <= num)
n++;
return n-1;
}
} // _Class