Binary Files · 11 / 14 Exercise Medium

Hexadecimal viewer

Objective

Create a hexadecimal viewer in Python that shows the contents of a binary file on screen as follows:

  • 16 bytes per row
  • 24 rows on screen
 

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.

Files: the file data.bin already exists. Show it 16 bytes per row. Each row has: the offset of the first byte of the row in 8 lowercase hexadecimal digits, two spaces, the 16 bytes as two lowercase hexadecimal digits separated by one space (padded with spaces up to 47 characters if the last row has fewer bytes), two spaces and, between vertical bars |, the characters of those bytes (bytes below 32 or above 126 are shown as a dot). Example: 00000000  48 65 6c 6c 6f 00 ff 41 42 43 44 45 46 47 48 49  |Hello..ABCDEFGHI|. After every 24 rows, wait for the user to press Enter (read it with input() without printing any message) before continuing.

Output
00000000  48 65 6c 6c 6f 00 ff 41 42 43 44 45 46 47 48 49  |Hello..ABCDEFGHI|

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise