Converter.java

Denna kod är public domain. Om ni hittar fel eller vill ändra något i koden blir jag jätteglad om ni skickar dessa ändringar till jesper [at] fantasi [punkt] se.


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

public class Converter extends Applet implements ActionListener
{
   public static final long serialVersionUID = 0;
   private TextField dec, hex, oct, bin, base, userDef;

   private static char hexChar(int n)
   {
      if (n < 10)
         return (char)(n + '0');
      return (char)(n - 10 + 'a');
   }

   private static int intChar(char c)
   { 
      if (c <= '9')
         return c - '0';
      return c - 'a' + 10;
   }

   public static String int2base(int n, int base)
   {
      String result = new String("");

      if (base == 1)
      {
         /* Specialfall för unära tal... */
         /* Vi definierar 0 unärt till den tomma strängen */
         if (n == 0)
            return result;
         else
            n--;
      }

      while (n >= base)
      {
         int t = n % base;
         if (base != 1)
            n /= base;
         else
            n--;
         result = hexChar(t) + result;
      }
      return hexChar(n) + result;
   }

   public static int base2int(String n, int base)
   {
      int result = 0;

      while (n.length() > 0)
      {
         result = result * base + intChar(n.charAt(0));
         n = n.substring(1);
      }
      return result;
   }

   public void actionPerformed(ActionEvent e)
   {
      int baseTen;

      if (e.getSource() == dec)
         baseTen = Integer.parseInt(dec.getText());
      else if (e.getSource() == hex)
         baseTen = base2int(hex.getText(), 16);
      else if (e.getSource() == oct)
         baseTen = base2int(oct.getText(), 8);
      else if (e.getSource() == bin)
         baseTen = base2int(bin.getText(), 2);
      else if (e.getSource() == userDef)
         baseTen = base2int(userDef.getText(), Integer.parseInt(base.getText()));
      else if (e.getSource() == base)
      {
         if (Integer.parseInt(base.getText()) <= 0)
            base.setText("1");
         userDef.setText(int2base(Integer.parseInt(dec.getText()),
                                  Integer.parseInt(base.getText())));
         return;
      }
      else return;

      dec.setText(""+baseTen);
      hex.setText(int2base(baseTen,16));
      oct.setText(int2base(baseTen,8));
      bin.setText(int2base(baseTen,2));
      userDef.setText(int2base(baseTen,Integer.parseInt(base.getText())));
   }

   public void init()
   {
      setLayout(new BorderLayout());

      Panel userBase = new Panel();
      userBase.add(new Label("Bas"));
      userBase.add(base = new TextField("5", 3));

      Panel labels = new Panel();
      labels.setLayout(new GridLayout(5, 1));
      labels.add(new Label("Decimalt"));
      labels.add(new Label("Hexadecimalt"));
      labels.add(new Label("Oktalt"));
      labels.add(new Label("Binärt"));
      labels.add(userBase);

      Panel textfields = new Panel();
      textfields.setLayout(new GridLayout(5, 1));
      textfields.add(dec = new TextField("0"));
      textfields.add(hex = new TextField("0"));
      textfields.add(oct = new TextField("0"));
      textfields.add(bin = new TextField("0"));
      textfields.add(userDef = new TextField("0"));

      add(labels, BorderLayout.WEST);
      add(textfields, BorderLayout.CENTER);

      dec.addActionListener(this);
      hex.addActionListener(this);
      oct.addActionListener(this);
      bin.addActionListener(this);
      base.addActionListener(this);
      userDef.addActionListener(this);

      setBackground(new Color(0xcc, 0xcc, 0xff));
      setVisible(true);
   }
}