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 |
<syntaxhighlight lang="julia">function baseddump(data::Vector{UInt8}; base = 16, offset = 0, len = -1, displayadjust = 0) |
||
@assert 2 <= base <= 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 > 8 ? 10 : base > 4 ? 8 : 6 |
|||
padsize = base == 16 ? 2 : base == 2 ? 8 : base > 7 ? 3 : base > 3 ? 4 : 5 |
|||
midpad = " "^(base != 2) |
|||
⚫ | |||
vl = (padsize + 1) * fullchunksize + length(midpad) |
|||
⚫ | |||
halflen, pos = fullchunksize ÷ 2, offset + displayadjust |
|||
⚫ | |||
⚫ | |||
pad = base == 16 ? 2 : base == 10 ? 3 : 8) * " ", chunk) |
|||
⚫ | |||
vstr = join(values[begin:begin+min(halflen, chunklen)-1]) |
|||
⚫ | |||
⚫ | |||
s1 = join(values[begin:begin+min(halflen, chunklen)-1]) |
|||
⚫ | |||
⚫ | |||
s1 *= midpad * join(values[begin+halflen:end]) |
|||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
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 |
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 |
xxd(data; offset = 0, len = -1) = baseddump(data; base = 2, offset, len) |
||
decdump(data; offset = 0, len = -1) = baseddump(data |
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 |
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.| |
||
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.| |
||
0000000c 01110100 00000000 01100001 00000000 00100000 00000000 |
0000000c 01110100 00000000 01100001 00000000 00100000 00000000 |t.a. .| |
||
00000012 01000011 00000000 01101111 00000000 01100100 00000000 |
00000012 01000011 00000000 01101111 00000000 01100100 00000000 |C.o.d.| |
||
00000018 01100101 00000000 00100000 00000000 01101001 00000000 |
00000018 01100101 00000000 00100000 00000000 01101001 00000000 |e. .i.| |
||
0000001e 01110011 00000000 00100000 00000000 01100001 00000000 |
0000001e 01110011 00000000 00100000 00000000 01100001 00000000 |s. .a.| |
||
00000024 00100000 00000000 01110000 00000000 01110010 00000000 |
00000024 00100000 00000000 01110000 00000000 01110010 00000000 | .p.r.| |
||
0000002a 01101111 00000000 01100111 00000000 01110010 00000000 |
0000002a 01101111 00000000 01100111 00000000 01110010 00000000 |o.g.r.| |
||
00000030 01100001 00000000 01101101 00000000 01101101 00000000 |
00000030 01100001 00000000 01101101 00000000 01101101 00000000 |a.m.m.| |
||
00000036 01101001 00000000 01101110 00000000 01100111 00000000 |
00000036 01101001 00000000 01101110 00000000 01100111 00000000 |i.n.g.| |
||
0000003c 00100000 00000000 01100011 00000000 01101000 00000000 |
0000003c 00100000 00000000 01100011 00000000 01101000 00000000 | .c.h.| |
||
00000042 01110010 00000000 01100101 00000000 01110011 00000000 |
00000042 01110010 00000000 01100101 00000000 01110011 00000000 |r.e.s.| |
||
00000048 01110100 00000000 01101111 00000000 01101101 00000000 |
00000048 01110100 00000000 01101111 00000000 01101101 00000000 |t.o.m.| |
||
0000004e 01100001 00000000 01110100 00000000 01101000 00000000 |
0000004e 01100001 00000000 01110100 00000000 01101000 00000000 |a.t.h.| |
||
00000054 01111001 00000000 00100000 00000000 01110011 00000000 |
00000054 01111001 00000000 00100000 00000000 01110011 00000000 |y. .s.| |
||
0000005a 01101001 00000000 01110100 00000000 01100101 00000000 |
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> |
||