Created
February 19, 2015 12:47
-
-
Save ralfw/d26ad56ee7310e89ae6d to your computer and use it in GitHub Desktop.
Brownfield Code aus dem Buch "Head First C#". Dank fürs Abtippen und Anpassen an Christian Andritzky.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace HexDumper | |
{ | |
using System.IO; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if (args.Length != 1) | |
{ | |
Console.Error.WriteLine("Verwendung: hexdumper Datei"); | |
Environment.Exit(1); | |
} | |
if (!File.Exists(args[0])) | |
{ | |
Console.Error.WriteLine("Keine derartige Datei: {0}", args[0]); | |
Environment.Exit(2); | |
} | |
using (Stream eingabe = File.OpenRead(args[0])) | |
{ | |
int position = 0; | |
byte[] puffer = new byte[16]; | |
while (position < eingabe.Length) | |
{ | |
int geleseneZeichen = eingabe.Read(puffer, 0, puffer.Length); | |
if (geleseneZeichen > 0) | |
{ | |
Console.Write("{0:X4}: ", position); | |
position += geleseneZeichen; | |
for (int i = 0; i < 16; i++) | |
{ | |
if (i < geleseneZeichen) | |
{ | |
string hex = string.Format("{0:x2}", puffer[i]); | |
Console.Write(hex + " "); | |
} | |
else | |
{ | |
Console.Write(" "); | |
} | |
if (i == 7) | |
{ | |
Console.Write("-- "); | |
} | |
if (puffer[i] < 32 || puffer[i] > 250) | |
{ | |
puffer[i] = (byte)'.'; | |
} | |
} | |
string pufferInhalt = Encoding.ASCII.GetString(puffer); | |
Console.WriteLine(" " + pufferInhalt.Substring(0, geleseneZeichen)); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment