Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 02, 2017 [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
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); } } |
August 02, 2017 Re: [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | 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
|
August 02, 2017 Re: [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick B | 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 |
August 02, 2017 Re: [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick B | 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 |
August 02, 2017 Re: [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | 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
|
August 02, 2017 Re: [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | 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 |
August 02, 2017 Re: [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | 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; } |
August 02, 2017 Re: [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | 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)))); } |
August 02, 2017 Re: [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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.
|
August 02, 2017 Re: [your code here] HexViewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | 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 |
Copyright © 1999-2021 by the D Language Foundation