www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [your code here] HexViewer

reply Andre Pany <andre s-e-a-p.de> writes:
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);
	}
}
Aug 02 2017
next sibling parent reply Nick B <nick.barbalich gmail.com> writes:
On Wednesday, 2 August 2017 at 19:39:18 UTC, Andre Pany wrote:
 This application opens the file passed as argument and display 
 the content in hex and text format:
Is this code in GitHub or DUB ? Is there a link ? Nick
Aug 02 2017
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/2/17 5:27 PM, Nick B wrote:
 On Wednesday, 2 August 2017 at 19:39:18 UTC, Andre Pany wrote:
 This application opens the file passed as argument and display the 
 content in hex and text format:
Is this code in GitHub or DUB ? Is there a link ?
Here is a link: https://forum.dlang.org/post/dvjltobyaaxoihhqywhp forum.dlang.org :) -Steve
Aug 02 2017
prev sibling parent "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Wed, Aug 02, 2017 at 09:27:07PM +0000, Nick B via Digitalmars-d wrote:
 On Wednesday, 2 August 2017 at 19:39:18 UTC, Andre Pany wrote:
 This application opens the file passed as argument and display the
 content in hex and text format:
 
Is this code in GitHub or DUB ? Is there a link ?
[...] The code is right there in the message. T -- Sometimes the best solution to morale problems is just to fire all of the unhappy people. -- despair.com
Aug 02 2017
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Aug 02 2017
prev sibling parent reply Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
On Wednesday, 2 August 2017 at 19:39:18 UTC, Andre Pany wrote:
 This application opens the file passed as argument and display 
 the content in hex and text format:
Good idea! But I think it needs more ranges: void main(string[] args) { import std.algorithm, std.format, std.stdio; enum cols = 16; args[1].File("rb").byChunk(16).map!(chunk => format!"%(%02X %)%*s %s"( chunk, 3 * (cols - chunk.length), "", chunk.map!(c => c < 0x20 || c > 0x7E ? '.' : char(c)))) .each!writeln; }
Aug 02 2017
next sibling parent reply Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
On Wednesday, 2 August 2017 at 21:59:23 UTC, Vladimir Panteleev 
wrote:
 Good idea! But I think it needs more ranges:
The format call can be substituted with writefln directly: void main(string[] args) { import std.algorithm, std.format, std.stdio; enum cols = 16; args[1].File("rb").byChunk(16).each!(chunk => writefln!"%(%02X %)%*s %s"( chunk, 3 * (cols - chunk.length), "", chunk.map!(c => c < 0x20 || c > 0x7E ? '.' : char(c)))); }
Aug 02 2017
next sibling parent Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
On Wednesday, 2 August 2017 at 22:02:49 UTC, Vladimir Panteleev 
wrote:
 On Wednesday, 2 August 2017 at 21:59:23 UTC, Vladimir Panteleev 
 wrote:
 Good idea!
https://github.com/dlang/dlang.org/pull/1854
Aug 02 2017
prev sibling next sibling parent Andre Pany <andre s-e-a-p.de> writes:
On Wednesday, 2 August 2017 at 22:02:49 UTC, Vladimir Panteleev 
wrote:
 On Wednesday, 2 August 2017 at 21:59:23 UTC, Vladimir Panteleev 
 wrote:
 Good idea! But I think it needs more ranges:
The format call can be substituted with writefln directly: void main(string[] args) { import std.algorithm, std.format, std.stdio; enum cols = 16; args[1].File("rb").byChunk(16).each!(chunk => writefln!"%(%02X %)%*s %s"( chunk, 3 * (cols - chunk.length), "", chunk.map!(c => c < 0x20 || c > 0x7E ? '.' : char(c)))); }
Really cool:) Kind regards André
Aug 02 2017
prev sibling parent reply Martin Tschierschke <mt smartdolphin.de> writes:
On Wednesday, 2 August 2017 at 22:02:49 UTC, Vladimir Panteleev 
wrote:
 On Wednesday, 2 August 2017 at 21:59:23 UTC, Vladimir Panteleev 
 wrote:
 Good idea! But I think it needs more ranges:
The format call can be substituted with writefln directly: void main(string[] args) { import std.algorithm, std.format, std.stdio; enum cols = 16; args[1].File("rb").byChunk(16).each!(chunk => writefln!"%(%02X %)%*s %s"( chunk, 3 * (cols - chunk.length), "", chunk.map!(c => c < 0x20 || c > 0x7E ? '.' : char(c)))); }
Very cool! Now you can remove: std.format, and I would substitute: byChunk(16) with byChunk(col) Can you point me to the explanation of this: %(%02X %)%*s %s ? Best regards mt.
Aug 03 2017
next sibling parent Martin Tschierschke <mt smartdolphin.de> writes:
On Thursday, 3 August 2017 at 08:47:12 UTC, Martin Tschierschke 
wrote:
 On Wednesday, 2 August 2017 at 22:02:49 UTC, Vladimir Panteleev 
 wrote:
 On Wednesday, 2 August 2017 at 21:59:23 UTC, Vladimir 
 Panteleev wrote:
 Good idea! But I think it needs more ranges:
The format call can be substituted with writefln directly: void main(string[] args) { import std.algorithm, std.format, std.stdio; enum cols = 16; args[1].File("rb").byChunk(16).each!(chunk => writefln!"%(%02X %)%*s %s"( chunk, 3 * (cols - chunk.length), "", chunk.map!(c => c < 0x20 || c > 0x7E ? '.' : char(c)))); }
Very cool! Now you can remove: std.format, and I would substitute: byChunk(16) with byChunk(col)
Error should be cols: byChunk(cols)
 Can you point me to the explanation of this: %(%02X %)%*s  %s  ?
Ah I found it myself: https://dlang.org/library/std/format/formatted_write.html The %( %) called "nested array formatting" was new for me.
Aug 03 2017
prev sibling parent Biotronic <simen.kjaras gmail.com> writes:
On Thursday, 3 August 2017 at 08:47:12 UTC, Martin Tschierschke 
wrote:
 Can you point me to the explanation of this: %(%02X %)%*s  %s  ?
https://dlang.org/phobos/std_format.html Under "Example using array and nested array formatting:" writefln("My items are %(%s %).", [1,2,3]); So "%( %)" is an array format specifier, with the stuff between the two as the element format. In the case of "%(%02X %)%*s %s", that reads: "%(" // Start array format specifier for arg 1. "%02X " // Format each element as a 2 digit hex number followed by a space. "%)" // End array format specifier. "%*s" // Right justify arg 3 (""), using width specified by arg 2. " %s" // And print arg 4 using no special format. -- Biotronic
Aug 03 2017
prev sibling parent reply "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Wed, Aug 02, 2017 at 09:59:23PM +0000, Vladimir Panteleev via Digitalmars-d
wrote:
 On Wednesday, 2 August 2017 at 19:39:18 UTC, Andre Pany wrote:
 This application opens the file passed as argument and display the
 content in hex and text format:
Good idea! But I think it needs more ranges: void main(string[] args) { import std.algorithm, std.format, std.stdio; enum cols = 16; args[1].File("rb").byChunk(16).map!(chunk => format!"%(%02X %)%*s %s"( chunk, 3 * (cols - chunk.length), "", chunk.map!(c => c < 0x20 || c > 0x7E ? '.' : char(c)))) .each!writeln; }
Whoa. This is cool and everything, but it also looks pretty intimidating for a newcomer to D. I'm not sure if we should put this on the front page! T -- Democracy: The triumph of popularity over principle. -- C.Bond
Aug 02 2017
parent Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
On Wednesday, 2 August 2017 at 21:58:18 UTC, H. S. Teoh wrote:
 Whoa. This is cool and everything, but it also looks pretty 
 intimidating for a newcomer to D.  I'm not sure if we should 
 put this on the front page!
Perhaps we should make some examples only available if the user selects them explicitly from the dropdown, i.e. never show them initially, and only use simpler examples for the initially-visible random example.
Aug 02 2017