Hex dump: Difference between revisions

Content added Content deleted
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: Line 413:


=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight lang="julia">function baseddump(data, base = 16; offset = 0, len = -1)
<syntaxhighlight lang="julia">function baseddump(data::Vector{UInt8}; base = 16, offset = 0, len = -1, displayadjust = 0)
@assert base in [2, 10, 16] "display base $base not supported"
@assert 2 <= base <= 16 "display base $base not supported"
len = len < 0 ? length(data) : min(len, length(data))
len = len < 0 ? length(data) : min(len, length(data))
bytes = data[begin+offset:len]
bytes = data[begin+offset:len]
fullchunksize = base == 16 ? 16 : base == 10 ? 10 : 6
fullchunksize = base == 16 ? 16 : base > 8 ? 10 : base > 4 ? 8 : 6
halflen, pos, vlen = fullchunksize ÷ 2, 0, base == 16 ? 49 : base == 10 ? 41 : 55
padsize = base == 16 ? 2 : base == 2 ? 8 : base > 7 ? 3 : base > 3 ? 4 : 5
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,
for chunk in Iterators.partition(bytes, fullchunksize)
pad = base == 16 ? 2 : base == 10 ? 3 : 8) * " ", chunk)
chunklen = length(chunk)
vstr = join(values[begin:begin+min(halflen, chunklen)-1])
values = map(n -> string(n, base = base, pad = padsize) * " ", chunk)
if chunklen > halflen
vstr *= " "^(base != 2) * join(values[begin+halflen:end])
s1 = join(values[begin:begin+min(halflen, chunklen)-1])
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 * "|")
s2 = prod(map(n -> n < 128 && isprint(Char(n)) ? Char(n) : '.', chunk))
pos += chunklen
println(string(pos, base = 16, pad = 8) * " " * rpad(s1, vl) * "|" * s2 * "|")
end
pos += chunklen
println(string(pos, base=16, pad = 8))
end
println(string(pos - offset - displayadjust, base = 16, pad = 8))
end
function baseddump(data; base = 16, offset = 0, len = -1)
return baseddump(vec(collect(reinterpret(UInt8, data))); base, offset, len)
end
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
end
hexdump(data; offset = 0, len = -1) = baseddump(data; offset, len)
hexdump(data; offset = 0, len = -1) = baseddump(data; offset, len)
xxd(data; offset = 0, len = -1) = baseddump(data, 2; offset, len)
xxd(data; offset = 0, len = -1) = baseddump(data; base = 2, offset, len)
decdump(data; offset = 0, len = -1) = baseddump(data, 10; offset, len)
decdump(data; offset = 0, len = -1) = baseddump(data; base = 10, offset, len)


const tstr = b"Rosetta Code is a programming chrestomathy site 😀."
const tstr = b"Rosetta Code is a programming chrestomathy site 😀."
Line 445: Line 459:
print("\ndecdump of utf-16 string "), display(String(tstr))
print("\ndecdump of utf-16 string "), display(String(tstr))
decdump(utf16)
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}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Line 455: Line 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.|
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
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 😀."
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.|
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.|
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.|
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.|
0000000c 01110100 00000000 01100001 00000000 00100000 00000000 |t.a. .|
0000000c 01110100 00000000 01100001 00000000 00100000 00000000 |t.a. .|
00000012 01000011 00000000 01101111 00000000 01100100 00000000 |C.o.d.|
00000012 01000011 00000000 01101111 00000000 01100100 00000000 |C.o.d.|
00000018 01100101 00000000 00100000 00000000 01101001 00000000 |e. .i.|
00000018 01100101 00000000 00100000 00000000 01101001 00000000 |e. .i.|
0000001e 01110011 00000000 00100000 00000000 01100001 00000000 |s. .a.|
0000001e 01110011 00000000 00100000 00000000 01100001 00000000 |s. .a.|
00000024 00100000 00000000 01110000 00000000 01110010 00000000 | .p.r.|
00000024 00100000 00000000 01110000 00000000 01110010 00000000 | .p.r.|
0000002a 01101111 00000000 01100111 00000000 01110010 00000000 |o.g.r.|
0000002a 01101111 00000000 01100111 00000000 01110010 00000000 |o.g.r.|
00000030 01100001 00000000 01101101 00000000 01101101 00000000 |a.m.m.|
00000030 01100001 00000000 01101101 00000000 01101101 00000000 |a.m.m.|
00000036 01101001 00000000 01101110 00000000 01100111 00000000 |i.n.g.|
00000036 01101001 00000000 01101110 00000000 01100111 00000000 |i.n.g.|
0000003c 00100000 00000000 01100011 00000000 01101000 00000000 | .c.h.|
0000003c 00100000 00000000 01100011 00000000 01101000 00000000 | .c.h.|
00000042 01110010 00000000 01100101 00000000 01110011 00000000 |r.e.s.|
00000042 01110010 00000000 01100101 00000000 01110011 00000000 |r.e.s.|
00000048 01110100 00000000 01101111 00000000 01101101 00000000 |t.o.m.|
00000048 01110100 00000000 01101111 00000000 01101101 00000000 |t.o.m.|
0000004e 01100001 00000000 01110100 00000000 01101000 00000000 |a.t.h.|
0000004e 01100001 00000000 01110100 00000000 01101000 00000000 |a.t.h.|
00000054 01111001 00000000 00100000 00000000 01110011 00000000 |y. .s.|
00000054 01111001 00000000 00100000 00000000 01110011 00000000 |y. .s.|
0000005a 01101001 00000000 01110100 00000000 01100101 00000000 |i.t.e.|
0000005a 01101001 00000000 01110100 00000000 01100101 00000000 |i.t.e.|
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...|
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...|
00000066 00101110 00000000 |..|
00000066 00101110 00000000 |..|
00000068
00000068


Line 491: Line 510:
00000064 000 222 046 000 |....|
00000064 000 222 046 000 |....|
00000068
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>
</pre>