[your code here] HexViewer
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Wed Aug 2 14:49:15 PDT 2017
On 8/2/17 3:39 PM, Andre Pany wrote:
> This application opens the file passed as argument and display the
> content in hex and text format:
>
> 00 00 03 00 00 00 64 00 00 00 FF 56 01 00 00 70 ......d... V..p
> 02 00 FF A6 00 00 00 20 02 00 00 00 00 00 00 00 . ยช... .......
> 00 00 00 00 00 00 00 00 00 00 00 00 ............
>
> void main(string[] args)
> {
> import std.file, std.string, std.range, std.array, std.algorithm,
> std.digest, std.conv;
> import std.stdio: writeln;
>
> enum cols = 16;
> auto data = cast(const(ubyte)[]) read(args[1]);
>
> foreach(g; data.chunks(cols))
> {
> string hex = g.toHexString.chunks(2).join(" ").to!string;
> string txt = g.map!(b => b == 0 ? '.' : char(b)).array;
> writeln(hex.leftJustify(cols * 2 + (cols - 1), ' '), " ", txt);
> }
> }
Very nice!
I think actually you are going to have a bit of trouble with the 'text'
output, since D is going to output the character array as unicode, vs. a
normal hexdump which will output as one glyph per byte. You could do
this poorly by changing the map condition to b == 0 || b >= 0x80.
Actually you may want to substitute some extra chars, as I'm not sure
low bytes are printable (or will print what you really want them to).
-Steve
More information about the Digitalmars-d
mailing list