Write a Hex Viewer method with the following header:
private static void viewHex(String filename)
The method reads bytes from filename and displays them in hex representation. The output should be formatted as in the example below, i.e., each line consists of 8 pairs of hex numbers, then ‘|’, then another 8 pairs. Use Integer.toHexString() to convert a byte into a string representing the equivalent hex. Use a try statement to handle IOException and display a simple error message if an I/O exception occurs.
/** * @fileName HexViewer.java * @author * @since 28/2/17 */ package hexviewer; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class HexViewer { /** * This method reads file as byte and print as a hex String * @param fileName */ public static void viewHex(String fileName) { Path path = Paths.get(fileName); byte[] data = new byte[0]; try { data = Files.readAllBytes(path); } catch (IOException e) { e.printStackTrace(); } int count = 1; for (byte b : data) { String hexString = Integer.toHexString(b); // System.out.println(String.format("%02X ", b)); if (count == 8) { System.out.print(hexString + "| "); count = 0; } else { System.out.print(hexString + " "); } count++; } System.out.println(); } public static void main(String[] args) { viewHex("input.txt"); } }
input.txt
output:
31 38 20 31 38 a 54 20| 50 20 49 20 52 20 43 20| 53 20 41 20 56 20 41 20| 4a 20 4c 20 45 20 58 20| 49 20 50 20 49 20 47 20| 45 a 4c 20 49 20 41 20| 4d 20 45 20 4d 20 4f 20| 52 20 59 20 4d 20 4d 20| 4f 20 55 20 53 20 45 20| 4e 20 49 20 4c a 43 20| 52 20 41 20 42 20 4b 20| 53 20 41 20 54 20 58 20| 49 20 4e 20 55 20 59 20| 48 20 53 20 54 20 46 20| 47 a 44 20 4e 20 44 20| 49 20 52 20 45 20 43 20| 54 20 4f 20 52 20 59 20| 45 20 54 20 41 20 4f 20| 45 20 4f 20 4f a 50 20| 4f 20 57 20 45 20 52 20| 53 20 55 20 50 20 50 20| 4c 20 59 20 4e 20 49 20| 52 20 46 20 52 20 4c 20| 4f a 55 20 43 20 4f 20| 41 20 53 20 41 20 45 20| 56 20 41 20 53 20 53 20| 43 20 52 20 45 20 54 20| 4e 20 44 20 47 a 4b 20| 49 20 52 20 4f 20 50 20| 4b 20 54 20 59 20 50 20| 53 20 48 20 52 20 55 20| 57 20 57 20 45 20 45 20| 4c a 43 20 44 20 44 20| 45 20 43 20 50 20 52 20| 45 20 45 20 41 20 48 20| 59 20 43 20 41 20 41 20| 54 20 52 20 4d a 41 20| 4e 20 52 20 49 20 4d 20| 41 20 4c 20 4c 20 54 20| 44 20 52 20 50 20 45 20| 52 20 52 20 45 20 41 20| 54 a 42 20 4f 20 4c 20| 45 20 4e 20 4d 20 45 20| 49 20 45 20 4b 20 45 20| 54 20 53 20 45 20 45 20| 50 20 48 20 48 a 52 20| 43 20 4b 20 49 20 50 20| 52 20 41 20 46 20 43 20| 56 20 52 20 49 20 49 20| 52 20 53 20 55 20 4c 20| 4d a 45 20 45 20 42 20| 45 20 49 20 41 20 52 20| 52 20 49 20 41 20 42 20| 4f 20 4f 20 54 20 4d 20| 42 20 4f 20 52 a 4e 20| 53 20 54 20 57 20 52 20| 41 20 50 20 52 20 47 20| 52 20 54 20 4e 20 57 20| 42 20 49 20 4e 20 47 20| 4f a 4e 20 4f 20 4f 20| 53 20 47 20 4e 20 44 20| 4c 20 4f 20 4f 20 44 20| 49 20 4e 20 54 20 49 20| 4f 20 49 20 53 a 41 20| 4e 20 47 20 4d 20 41 20| 4b 20 41 20 55 20 4c 20| 41 20 52 20 41 20 4f 20| 54 20 45 20 41 20 4e 20| 52 a 43 20 41 20 45 20| 41 20 53 20 50 20 54 20| 4c 20 54 20 41 20 49 20| 50 20 4f 20 4e 20 52 20| 4e 20 44 20 55 a 53 20| 4e 20 46 20 49 20 52 20| 45 20 57 20 41 20 4c 20| 4c 20 57 20 52 20 45 20| 49 20 4b 20 4f 20 4f 20| 43 a 54 20 46 20 44 20| 50 20 52 20 44 20 48 20| 54 20 4f 20 4f 20 54 20| 45 20 55 20 4c 20 42 20| 59 20 54 20 45 a 4a 41| 56 41 53 43 52 49 50 54| a 50 49 58 45 4c a 49| 4e 54 45 52 4e 45 54 a| 47 49 46 a 47 4f 4f 47| 4c 45 a 4c 43 44 a 45| 4d 41 49 4c a 4d 45 4d| 4f 52 59 a 4d 4f 55 53| 45 a 53 48 41 52 45 57| 41 52 45 a 54 41 53 4b| 42 41 52 a 55 4e 49 58| a 53 45 43 55 52 49 54| 59 a 53 4f 46 54 57 41| 52 45 a 46 4f 4c 44 45| 52 a 49 43 4f 4e a 44| 49 52 45 43 54 4f 52 59|
Get Answers For Free
Most questions answered within 1 hours.