Hex dump: Difference between revisions
m
→{{header|Julia}}: add file usage
m (→{{header|Python}}: Fix offset and final byte count when skipping bytes. Show example of using offset and length.) |
m (→{{header|Julia}}: add file usage) |
||
Line 413:
=={{header|Julia}}==
<syntaxhighlight lang="julia">function baseddump(data
midpad = " "^(base != 2)
for chunk in Iterators.partition(bytes, fullchunksize)▼
vl = (padsize + 1) * fullchunksize + length(midpad)
chunklen = length(chunk)▼
halflen, pos = fullchunksize ÷ 2, offset + displayadjust
values = map(n -> string(n, base = base,▼
if chunklen > halflen▼
end▼
s1 *= midpad * join(values[begin+halflen:end])
cstr = prod(map(n -> n < 128 && isprint(Char(n)) ? Char(n) : '.', chunk))▼
end
println(string(pos, base = 16, pad = 8) * " " * rpad(vstr, vlen) * "|" * cstr * "|")▼
pos += chunklen▼
end▼
println(string(pos, base=16, pad = 8))▼
end
▲ println(string(pos - offset - displayadjust, base = 16, pad = 8))
function baseddump(data; base = 16, offset = 0, len = -1)
return baseddump(vec(collect(reinterpret(UInt8, data))); base, offset, len)
function baseddump(filename::AbstractString; base = 16, offset = 0, len = -1)
fromio = open(filename)
flen = stat(fromio).size
len = len < 0 ? flen - offset : min(len, flen - offset)
offset != 0 && seek(fromio, offset)
data::Vector{UInt8} = read(fromio, len)
baseddump(data; base = base, offset = 0, displayadjust = offset)
close(fromio)
end
hexdump(data; offset = 0, len = -1) = baseddump(data; offset, len)
xxd(data; offset = 0, len = -1) = baseddump(data
decdump(data; offset = 0, len = -1) = baseddump(data
const tstr = b"Rosetta Code is a programming chrestomathy site 😀."
Line 445 ⟶ 459:
print("\ndecdump of utf-16 string "), display(String(tstr))
decdump(utf16)
println("\nbaseddump of utf-16 string bytes 16-56 from file:")
fname = tempname()
open(fname, "w") do fd; write(fd, utf16) end
baseddump(fname, offset = 16, len = 40)
</syntaxhighlight>{{out}}
<pre>
Line 455 ⟶ 474:
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 | .=.....|
00000068
xxd of utf-16 string "Rosetta Code is a programming chrestomathy site 😀."
00000000 11111111 11111110 01010010 00000000 01101111 00000000
00000006 01110011 00000000 01100101 00000000 01110100 00000000
0000000c 01110100 00000000 01100001 00000000 00100000 00000000
00000012 01000011 00000000 01101111 00000000 01100100 00000000
00000018 01100101 00000000 00100000 00000000 01101001 00000000
0000001e 01110011 00000000 00100000 00000000 01100001 00000000
00000024 00100000 00000000 01110000 00000000 01110010 00000000
0000002a 01101111 00000000 01100111 00000000 01110010 00000000
00000030 01100001 00000000 01101101 00000000 01101101 00000000
00000036 01101001 00000000 01101110 00000000 01100111 00000000
0000003c 00100000 00000000 01100011 00000000 01101000 00000000
00000042 01110010 00000000 01100101 00000000 01110011 00000000
00000048 01110100 00000000 01101111 00000000 01101101 00000000
0000004e 01100001 00000000 01110100 00000000 01101000 00000000
00000054 01111001 00000000 00100000 00000000 01110011 00000000
0000005a 01101001 00000000 01110100 00000000 01100101 00000000
00000060 00100000 00000000 00111101 11011000 00000000 11011110
00000066 00101110 00000000
00000068
Line 491 ⟶ 510:
00000064 000 222 046 000 |....|
00000068
baseddump of utf-16 string bytes 16-56 from file:
00000010 20 00 43 00 6f 00 64 00 65 00 20 00 69 00 73 00 | .C.o.d.e. .i.s.|
00000020 20 00 61 00 20 00 70 00 72 00 6f 00 67 00 72 00 | .a. .p.r.o.g.r.|
00000030 61 00 6d 00 6d 00 69 00 |a.m.m.i.|
00000028
</pre>
|