Create a hexadecimal viewer in Java that shows the contents of a binary file on screen as follows:
The 16 bytes will be displayed first in hexadecimal and then as printable characters. You must also substitute bytes smaller than 32 (unprintable characters) with dots.
Display 24 rows of 16 bytes each time the user presses Enter.
00000000 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00 MZ?.........ÿÿ.. 00000010 b8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 ¸.......@....... 00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000030 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 00 ............?... 00000040 0e 1f ba 0e 00 b4 09 cd 21 b8 01 4c cd 21 54 68 ..º..´.Í!¸.LÍ!Th 00000050 69 73 20 70 72 6f 67 72 61 6d 20 63 61 6e 6e 6f is program canno 00000060 74 20 62 65 20 72 75 6e 20 69 6e 20 44 4f 53 20 t be run in DOS 00000070 6d 6f 64 65 2e 0d 0d 0a 24 00 00 00 00 00 00 00 mode....$....... 00000080 50 45 00 00 4c 01 03 00 b2 7c cd cf 00 00 00 00 PE..L...²|ÍÏ.... 00000090 00 00 00 00 e0 00 22 00 0b 01 30 00 00 0a 00 00 ....à."...0..... 000000a0 00 08 00 00 00 00 00 00 66 28 00 00 00 20 00 00 ........f(... .. 000000b0 00 40 00 00 00 00 40 00 00 20 00 00 00 02 00 00 .@....@.. ...... 000000c0 04 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00 ................ 000000d0 00 80 00 00 00 02 00 00 00 00 00 00 03 00 60 85 .?............`? 000000e0 00 00 10 00 00 10 00 00 00 00 10 00 00 10 00 00 ................ 000000f0 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00 ................ 00000100 13 28 00 00 4f 00 00 00 00 40 00 00 bc 05 00 00 .(..O....@..¼... 00000110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000120 00 60 00 00 0c 00 00 00 6c 27 00 00 38 00 00 00 .`......l''..8... 00000130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000150 00 00 00 00 00 00 00 00 00 20 00 00 08 00 00 00 ......... ...... 00000160 00 00 00 00 00 00 00 00 08 20 00 00 48 00 00 00 ......... ..H... 00000170 00 00 00 00 00 00 00 00 2e 74 65 78 74 00 00 00 .........text...
import java.io.*; public class HexViewer { public static void main(String[] args) { final int BUFFER_SIZE = 16; String fileName = "app.exe"; try (FileInputStream file = new FileInputStream(fileName)) { byte[] data = new byte[BUFFER_SIZE]; int bytesRead; int rowCount = 0; StringBuilder hexLine = new StringBuilder(); StringBuilder charLine = new StringBuilder(); do { System.out.print(String.format("%08X", file.getChannel().position())); System.out.print(" "); bytesRead = file.read(data, 0, BUFFER_SIZE); for (int i = 0; i < bytesRead; i++) { System.out.print(String.format("%02X", data[i]) + " "); if (data[i] < 32) { charLine.append('.'); } else { charLine.append((char) data[i]); } } for (int i = bytesRead; i < BUFFER_SIZE; i++) { System.out.print(" "); } System.out.println(charLine.toString()); charLine.setLength(0); rowCount++; if (rowCount == 24) { System.out.println("Press Enter to continue..."); try { System.in.read(); } catch (Exception e) { e.printStackTrace(); } rowCount = 0; } } while (bytesRead == BUFFER_SIZE); } catch (IOException e) { e.printStackTrace(); } } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Create a Java program that reads ID3 v1 specification tags from an MP3 music file.
Create a program in Java to read the dimensions of an image in Windows bitmap format.
Create a program in Java to encrypt an image in Windows bitmap format.
Create a program in Java that inverts all the bytes of a binary file.
Create a Java program that makes copies of both text and binary files.
Create a Java program that divides text or binary files into parts of 5 Kb each.
Create a hexadecimal viewer in Java that shows the contents of a binary file on screen as follows.
Practice with exercises in Java to manage binary files. Learn how to create, update, and search binary files in different ways.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.