/* BaseConverter.java Dave Klick CIS 160 2014-09-11 Converts a number between bases using command line arguments. */ import java.util.Scanner; import java.util.HashMap; public class BaseConverter { public static void main(String[] args) { Scanner in = new Scanner(System.in); int baseFrom = 10, baseTo = 10, number = 0; HashMap map = new HashMap() {{ put('0', "0000"); put('1', "0001"); put('2', "0010"); put('3', "0011"); put('4', "0100"); put('5', "0101"); put('6', "0110"); put('7', "0111"); put('8', "1000"); put('9', "1001"); put('A', "1010"); put('B', "1011"); put('C', "1100"); put('D', "1101"); put('E', "1110"); put('F', "1111"); }}; if (args.length != 3) { System.err.println("Usage: java BaseConverter number baseFrom baseTo"); System.exit(1); } try { baseFrom = Integer.parseInt(args[1], 10); if (baseFrom < 2 || baseFrom > 16) throw new NumberFormatException("Base out of Range"); } catch (NumberFormatException e) { System.err.println("Invalid base to convert from: " + args[1]); System.exit(2); } try { baseTo = Integer.parseInt(args[2], 10); if (baseTo != 2 && baseTo != 8 && baseTo != 10 && baseTo != 16) throw new NumberFormatException("Base out of Range"); } catch (NumberFormatException e) { System.err.println("Invalid base to convert to: " + args[2]); System.exit(3); } try { number = Integer.parseInt(args[0], baseFrom); } catch (NumberFormatException e) { System.err.println("Invalid number to convert: " + args[0]); System.exit(4); } String strNumOut = ""; switch (baseTo) { case 2: String temp = String.format("%X", number); for (int i=0; i