- Course
- Binary Files
- Hexadecimal viewer
Hexadecimal viewer
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a hexadecimal viewer in C# 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.
Input
Output
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...
Solution
using System;
using System.IO;
public class HexadecimalViewer
{
public static void Main(string[] args)
{
const int SIZE_BUFFER = 16;
string fileName = "app.exe";
using (FileStream file = File.OpenRead(fileName))
{
byte[] data = new byte[SIZE_BUFFER];
int amount;
int c = 0;
string line = string.Empty;
do
{
Console.Write(ToHex(file.Position, 8));
Console.Write(" ");
amount = file.Read(data, 0, SIZE_BUFFER);
for (int i = 0; i < amount; i++)
{
Console.Write(ToHex(data[i], 2) + " ");
if (data[i] < 32)
{
line += ".";
}
else
{
line += (char)data[i];
}
}
if (amount < SIZE_BUFFER)
{
for (int i = amount; i < SIZE_BUFFER; i++)
{
Console.Write(" ");
}
}
Console.WriteLine(line);
line = "";
c++;
if (c == 24)
{
Console.ReadLine();
c = 0;
}
}
while (amount == SIZE_BUFFER);
}
}
public static string ToHex(long n, int digits)
{
string hex = Convert.ToString(n, 16);
while (hex.Length < digits)
{
hex = "0" + hex;
}
return hex;
}
}