Hex dump: Difference between revisions
Content deleted Content added
m →{{header|Julia}}: output syntax |
m →{{header|Julia}}: add final count |
||
Line 239: | Line 239: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia">function baseddump(data, base = 16; offset = 0, len = -1) |
<syntaxhighlight lang="julia">function baseddump(data, base = 16; offset = 0, len = -1) |
||
@assert base in [2, 10, 16] "display base $base not supported" |
|||
len = len < 0 ? length(data) : min(len, length(data)) |
|||
bytes = data[begin+offset:len] |
|||
fullchunksize = base == 16 ? 16 : base == 10 ? 10 : 6 |
|||
halflen, pos, vlen = fullchunksize ÷ 2, 0, base == 16 ? 49 : base == 10 ? 41 : 55 |
|||
for chunk in Iterators.partition(bytes, fullchunksize) |
|||
chunklen = length(chunk) |
|||
values = map(n -> string(n, base = base, |
|||
pad = base == 16 ? 2 : base == 10 ? 3 : 8) * " ", chunk) |
|||
vstr = join(values[begin:begin+min(halflen, chunklen)-1]) |
|||
if chunklen > halflen |
|||
vstr *= " "^(base != 2) * join(values[begin+halflen:end]) |
|||
end |
|||
cstr = prod(map(n -> n < 128 && isprint(Char(n)) ? Char(n) : '.', chunk)) |
|||
println(string(pos, base = 16, pad = 8) * " " * rpad(vstr, vlen) * "|" * cstr * "|") |
|||
pos += chunklen |
|||
end |
|||
println(string(pos, base=16, pad = 8)) |
|||
end |
end |
||
hexdump(data; offset = 0, len = -1) = baseddump(data; offset, len) |
hexdump(data; offset = 0, len = -1) = baseddump(data; offset, len) |
||
Line 269: | Line 270: | ||
print("\ndecdump of utf-16 string "), display(String(tstr)) |
print("\ndecdump of utf-16 string "), display(String(tstr)) |
||
decdump(utf16) |
decdump(utf16) |
||
</syntaxhighlight>{{out}} |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Line 279: | Line 279: | ||
00000040 68 00 72 00 65 00 73 00 74 00 6f 00 6d 00 61 00 |h.r.e.s.t.o.m.a.| |
00000040 68 00 72 00 65 00 73 00 74 00 6f 00 6d 00 61 00 |h.r.e.s.t.o.m.a.| |
||
00000050 74 00 68 00 79 00 20 00 73 00 69 00 74 00 65 00 |t.h.y. .s.i.t.e.| |
00000050 74 00 68 00 79 00 20 00 73 00 69 00 74 00 65 00 |t.h.y. .s.i.t.e.| |
||
00000060 20 00 3d d8 00 de 2e 00 | .=.....| |
00000060 20 00 3d d8 00 de 2e 00 | .=.....| |
||
00000068 |
|||
xxd of utf-16 string "Rosetta Code is a programming chrestomathy site 😀." |
xxd of utf-16 string "Rosetta Code is a programming chrestomathy site 😀." |
||
Line 300: | Line 301: | ||
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...| |
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...| |
||
00000066 00101110 00000000 |..| |
00000066 00101110 00000000 |..| |
||
00000068 |
|||
decdump of utf-16 string "Rosetta Code is a programming chrestomathy site 😀." |
decdump of utf-16 string "Rosetta Code is a programming chrestomathy site 😀." |
||
Line 313: | Line 315: | ||
0000005a 105 000 116 000 101 000 032 000 061 216 |i.t.e. .=.| |
0000005a 105 000 116 000 101 000 032 000 061 216 |i.t.e. .=.| |
||
00000064 000 222 046 000 |....| |
00000064 000 222 046 000 |....| |
||
00000068 |
|||
</pre> |
</pre> |
||