Skip to content

Commit 6476dd2

Browse files
authored
Merge pull request #1854 from CyberShadow/pull-20170802-220958
Add hex viewer runnable example
2 parents 6025366 + 1404e48 commit 6476dd2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

index.dd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ void main()
123123
}
124124
----
125125
)
126+
$(EXTRA_EXAMPLE_RUNNABLE Print hex dump,
127+
----
128+
void main()
129+
{
130+
import std.algorithm, std.stdio, std.file;
131+
enum cols = 16;
132+
// Split file into 16-byte chunks per row
133+
thisExePath.File("rb").byChunk(cols).each!(chunk =>
134+
// Use range formatting to format the
135+
// hexadecimal part and align the text part
136+
writefln!"%(%02X %)%*s %s"(
137+
chunk,
138+
3 * (cols - chunk.length), "", // Padding
139+
chunk.map!(c => // Replace non-printable
140+
c < 0x20 || c > 0x7E ? '.' : char(c))));
141+
}
142+
----
143+
)
126144
$(EXTRA_EXAMPLE Start a minimal web server,
127145
----
128146
#!/usr/bin/env dub

0 commit comments

Comments
 (0)