Visor hexadecimal
Última actualización:
23/08/2020
⁃
Dificultad:
Intermedio
Cree un visor hexadecimal en C# que muestre el contenido de un archivo binario en pantalla de la siguiente manera:
- 16 bytes por cada fila
- 24 filas en pantalla
Los 16 bytes se mostrarán primero en hexadecimal y luego como caracteres imprimibles. Además deberás sustituir los bytes menores a 32 (caracteres no imprimibles) por puntos.
Muestre en pantalla 24 filas de 16 bytes cada vez que el usuario presione Enter.
Entrada
Salida
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...
Solución
using System;
using System.IO;
public class VisorHexadecimal
{
public static void Main(string[] args)
{
const int TAMANYO_BUFFER = 16;
string nombreArchivo = "app.exe";
using (FileStream archivo = File.OpenRead(nombreArchivo))
{
byte[] datos = new byte[TAMANYO_BUFFER];
int cantidad;
int c = 0;
string linea = string.Empty;
do
{
Console.Write(ToHex(archivo.Position, 8));
Console.Write(" ");
cantidad = archivo.Read(datos, 0, TAMANYO_BUFFER);
for (int i = 0; i < cantidad; i++)
{
Console.Write(ToHex(datos[i], 2) + " ");
if (datos[i] < 32)
{
linea += ".";
}
else
{
linea += (char)datos[i];
}
}
if (cantidad < TAMANYO_BUFFER)
{
for (int i = cantidad; i < TAMANYO_BUFFER; i++)
{
Console.Write(" ");
}
}
Console.WriteLine(linea);
linea = "";
c++;
if (c == 24)
{
Console.ReadLine();
c = 0;
}
}
while (cantidad == TAMANYO_BUFFER);
}
}
public static string ToHex(long n, int digits)
{
string hex = Convert.ToString(n, 16);
while (hex.Length < digits)
{
hex = "0" + hex;
}
return hex;
}
}