Base Converter

<< Blocking Game | LabTrailIndex | Synthesizer >>

Here is a starter File for the class that contains the library of functions that will eventually be in BaseConverter.jar which will be used by the BaseConverterGUI class.
 * The class BaseConverter will be a library of static methods
 * to convert from one base to another.
 * @author (type your name here) 
 * @version (type the date here)
class BaseConverter
    public static String fromHexToBin(String hexNum)
    	String binNum = "Not Yet Implemented";

    	return binNum;

    public static String fromBinToDec(String binNum)
    	int decNum = 0;

    	String result=String.valueOf(decNum); 
    	return "Not yet implemented";

    public static String fromBinToHex(String binNum)
    	String hexNum = "Not yet implemented";	

    	return hexNum;

    public static String fromDecToAny(String dec, int desiredBase)
    	int decNum=Integer.valueOf(dec);
        //Not yet implemented

    	return Integer.toString(decNum);       	

    public static String fromAnyToDec(String num, int base) 
    	return "Not yet implemented";

 * BaseConversionGUI uses the BaseConverter library
 * you make.  It should be in the
 * +libs folder and be called BaseConverter.jar
 * @author Fr Chris Thiel OFMCap
 * @version 1.0 28 Jan 2007

import java.awt.*;
import java.awt.event.*;

class BaseConversionGUI extends Frame
                implements ActionListener {

    TextField input;
    TextField output;
    Checkbox cb1, cb2, cb3, cb4, cb5, cb6;
    CheckboxGroup radio;
    Button convert;

    public BaseConversionGUI() {

        radio = new CheckboxGroup();
        cb1 = new Checkbox("Hex to Bin", radio, true);
        cb2 = new Checkbox("Bin to Dec", radio, false);
        cb3 = new Checkbox("Bin to Hex", radio, false);
        cb4 = new Checkbox("Dec to Bin", radio, false);
        cb5 = new Checkbox("Dec to Oct", radio, false);
        cb6 = new Checkbox("Dec to Hex", radio, false);
        input  = new TextField(25);
        output = new TextField(25);
        convert   = new Button("Convert ->");

        setLayout(new FlowLayout());
        Panel p = new Panel(); // Put the selection in a Panel
        p.setLayout(new GridLayout(0,1)); // no rows, one column


            (new WindowListener() {
                public void windowClosing(WindowEvent e) {
                public void windowClosed(WindowEvent e) {
                public void windowOpened(WindowEvent e) {
                public void windowIconified(WindowEvent e) {
                public void windowDeiconified(WindowEvent e) {
                public void windowActivated(WindowEvent e) {
                public void windowDeactivated(WindowEvent e) {


    public void actionPerformed(ActionEvent ev) {
        String userInput = input.getText();
        if (userInput.length()>0){
            Checkbox selected = radio.getSelectedCheckbox();
            if (selected  == cb1 )
            if (selected  == cb2 )
            if (selected  == cb3 )
            if (selected  == cb4 )
            if (selected  == cb5 )
            if (selected  == cb6 )


    public static void main(String args[]) {

        BaseConversionGUI app = new BaseConversionGUI();



Unit Testing

Ok, I've been holding out. There is such a thing called "eXtreme programming methodology" where before you write your method, you would write the code that tests your method FIRST! This is the same idea you read about in Chapter 16 where you would first look at what exactly goes into the method, and what you would like to come out. I have done this using the the println statements in the Lab16Test class, but this is the they you would do it using JUnit (Which BlueJ supports, by the way... it probably works in JCreator, but I haven't tried it... I'm sure it CAN work, but it might require some change of settings) To use the following, add this the the same Project that has the BaseConverter class, compile and select Run All Tests. (The power of JUnit is wasted on methods that pass and return Strings--will actully work on any Object!)

To Use with Eclipse, you must first add the junit.jar to your project, much like you added the gridworld.jar to your case study projects. Select your Eclipse project and right-click to select Properties. Click once on Java build Path, then click once on Libraries. Touch the Add External JARs, and search for junit. There should be many versions of junit.jar, just pick the first, and it probably will be the latest version (at the time I am writing it is was in the eclipse/plugins/org.junit4_4.3.1 folder). Now you can add a new Class to your project, name it BaseConverterTest, and paste the code below into it.

public class BaseConverterTest extends junit.framework.TestCase

	public void testFromHexToBin()
		assertEquals("11110000", BaseConverter.fromHexToBin("F0"));
		assertEquals("10101011110011010001001000110100", BaseConverter.fromHexToBin("ABCD1234"));
		assertEquals("11100001001010110100011111110101", BaseConverter.fromHexToBin("E12B47F5"));

	public void testFromBinToDec()
        assertEquals("48879", BaseConverter.fromBinToDec("1011111011101111"));
	    assertEquals("117046", BaseConverter.fromBinToDec("11100100100110110"));

	public void testFromBinToHex()
        assertEquals("BEEF",  BaseConverter.fromBinToHex("1011111011101111"));
        assertEquals("1C936", BaseConverter.fromBinToHex("11100100100110110"));

	public void testFromDecToAny()
		assertEquals("1000", BaseConverter.fromDecToAny("125", 5));
		assertEquals("1000", BaseConverter.fromDecToAny("512", 8));
		assertEquals("11001000", BaseConverter.fromDecToAny("200", 2));
		assertEquals("BEEF", BaseConverter.fromDecToAny("48879", 16));
		assertEquals("111", BaseConverter.fromDecToAny("7", 2));

	public void testFromAnyToDec()
		assertEquals("19", BaseConverter.fromAnyToDec("23", 8));
		assertEquals("511", BaseConverter.fromAnyToDec("777", 8));
		assertEquals("7", BaseConverter.fromAnyToDec("111", 2));
		assertEquals("15", BaseConverter.fromAnyToDec("F", 16));
		assertEquals("255", BaseConverter.fromAnyToDec("FF", 16));
		assertEquals("120", BaseConverter.fromAnyToDec("AA", 11));



Divide the Base, and keep track of the remainder. For example to convert 3157 into base 5: 3157 / 5 = 631 r 2

 631 / 5 = 126 r 1
 126 / 5 =  25 r 1
  25 / 5 =   5 r 0
   5 / 5 =   1 r 0
   1 / 5 =   0 r 1

Reading the remainders backwards, the base 5 version is 100112 Many of you will have no trouble until you get to base 16 when the remainder might be greater than 9 (a double digit remainder, in other words)! Here is cute trick. Have a string where the index of the digit matches the hex digit:

String digits="0123456789ABCDEF"; Now going from base 10 to 16, digits.charAt(10)="A", digits.charAt(12)="C" and digits.charAt(15)="F" This trick is also helpful in going in the other direction from Base 16 to Base 10: digits.indexOf("A") is 10, digits.indexOf("C") is 12, digits.indexOf("F") is 15.