Dec to Hex Converter


[Up]

Source Code

import java.lang.Integer;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.applet.Applet;

public class D2H extends Applet implements ActionListener {
  TextField t1, t2, t3;
  public void init() {
    t1 = new TextField(5);
    t1.addActionListener(this);
    t2 = new TextField(5);
    t2.addActionListener(this);
    t3 = new TextField(5);
    t3.addActionListener(this);
    add(new Label("Dec"));
    add(t1);
    add(new Label("<-> Hex"));
    add(t2);
    add(new Label("<-> Oct"));
    add(t3);
  }
  public void actionPerformed(ActionEvent evt) {
    Object o = evt.getSource();
    int i;
    if (o == t1) {
      t1.selectAll();
      i = Integer.parseInt(t1.getText());
      t2.setText(Integer.toHexString(i));
      t3.setText(Integer.toOctalString(i));
    }
    else if (o == t2) {
      t2.selectAll();
      i = Integer.parseInt(t2.getText(), 16);
      t1.setText(Integer.toString(i));
      t3.setText(Integer.toOctalString(i));
    }
    else {
      t3.selectAll();
      i = Integer.parseInt(t3.getText(), 8);
      t1.setText(Integer.toString(i));
      t2.setText(Integer.toHexString(i));
    }
  }
}

naniwa@rbt.his.fukui-u.ac.jp