[your code here] HexViewer

Andre Pany via Digitalmars-d digitalmars-d at puremagic.com
Wed Aug 2 12:39:18 PDT 2017


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);
	}
}


More information about the Digitalmars-d mailing list