Created
July 30, 2019 18:59
-
-
Save michaelpass/f7cf992e01e65f5729e7fa7aba7b3da9 to your computer and use it in GitHub Desktop.
Revisions
-
michaelpass created this gist
Jul 30, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ import java.io.*; private static void convertFileOutputToHex(String fileName) throws IOException { ArrayList<String> programLines = new ArrayList<>(); try { FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); while (br.ready()) { programLines.add(br.readLine()); } } catch (IOException e) { System.out.println("Error - Cannot find binary string input file."); } FileWriter fw = new FileWriter("prog.hex"); for (String line : programLines) { // Pad with leading 0's until while((line.length() % 4) != 0){ line = "0" + line; } // Convert binary digits to hex 4 at a time. String hexStringToPrint = ""; for(int n = 0; n < line.length(); n = n + 4){ // Construct quadlet String quadlet = String.valueOf(line.charAt(n + 0)) + String.valueOf(line.charAt(n + 1)) + String.valueOf(line.charAt(n + 2)) + String.valueOf(line.charAt(n + 3)); hexStringToPrint = hexStringToPrint + convertBinaryToHex(quadlet); } fw.write(hexStringToPrint); fw.write("\n"); } fw.close(); } private static String convertBinaryToHex(String binaryQuad){ Map<String, String> hexLookUpTable = new HashMap<>(); hexLookUpTable.put("0000", "0"); hexLookUpTable.put("0001", "1"); hexLookUpTable.put("0010", "2"); hexLookUpTable.put("0011", "3"); hexLookUpTable.put("0100", "4"); hexLookUpTable.put("0101", "5"); hexLookUpTable.put("0110", "6"); hexLookUpTable.put("0111", "7"); hexLookUpTable.put("1000", "8"); hexLookUpTable.put("1001", "9"); hexLookUpTable.put("1010", "A"); hexLookUpTable.put("1011", "B"); hexLookUpTable.put("1100", "C"); hexLookUpTable.put("1101", "D"); hexLookUpTable.put("1110", "E"); hexLookUpTable.put("1111", "F"); return hexLookUpTable.get(binaryQuad); }