Hex dump: Difference between revisions
Content deleted Content added
m →{{header|Phix}}: much cleaner way |
julia example |
||
Line 236: | Line 236: | ||
[https://jlaasonen.files.wordpress.com/2023/09/hexfile-1.3.1-windows-terminal-screenshot.png hexfile Windows terminal] |
[https://jlaasonen.files.wordpress.com/2023/09/hexfile-1.3.1-windows-terminal-screenshot.png hexfile Windows terminal] |
||
=={{header|Julia}}== |
|||
<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 = fullchunksize ÷ 2, 0 |
|||
for chunk in Iterators.partition(bytes, fullchunksize) |
|||
chunklen, vlen = length(chunk), base == 16 ? 49 : base == 10 ? 41 : 55 |
|||
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 -> isprint(Char(n)) ? Char(n) : '.', chunk)) |
|||
println(string(pos, base= 16, pad = 8) * " " * rpad(vstr, vlen) * "|" * cstr * "|") |
|||
pos += fullchunksize |
|||
end |
|||
end |
|||
hexdump(data; offset = 0, len = -1) = baseddump(data; offset, len) |
|||
xxd(data; offset = 0, len = -1) = baseddump(data, 2; offset, len) |
|||
decdump(data; offset = 0, len = -1) = baseddump(data, 10; offset, len) |
|||
const test = b"Rosetta Code is a programming chrestomathy site 😀." |
|||
print("hexdump of "), display(String(test)) |
|||
hexdump(test) |
|||
print("\nxxd of "), display(String(test)) |
|||
xxd(test) |
|||
print("\ndecdump of "), display(String(test)) |
|||
decdump(test) |
|||
</syntaxhighlight>{{out}} |
|||
<pre> |
|||
hexdump of "Rosetta Code is a programming chrestomathy site 😀." |
|||
00000000 52 6f 73 65 74 74 61 20 43 6f 64 65 20 69 73 20 |Rosetta Code is | |
|||
00000010 61 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 63 68 |a programming ch| |
|||
00000020 72 65 73 74 6f 6d 61 74 68 79 20 73 69 74 65 20 |restomathy site | |
|||
00000030 f0 9f 98 80 2e |ð....| |
|||
xxd of "Rosetta Code is a programming chrestomathy site 😀." |
|||
00000000 01010010 01101111 01110011 01100101 01110100 01110100 |Rosett| |
|||
00000006 01100001 00100000 01000011 01101111 01100100 01100101 |a Code| |
|||
0000000c 00100000 01101001 01110011 00100000 01100001 00100000 | is a | |
|||
00000012 01110000 01110010 01101111 01100111 01110010 01100001 |progra| |
|||
00000018 01101101 01101101 01101001 01101110 01100111 00100000 |mming | |
|||
0000001e 01100011 01101000 01110010 01100101 01110011 01110100 |chrest| |
|||
00000024 01101111 01101101 01100001 01110100 01101000 01111001 |omathy| |
|||
0000002a 00100000 01110011 01101001 01110100 01100101 00100000 | site | |
|||
00000030 11110000 10011111 10011000 10000000 00101110 |ð....| |
|||
decdump of "Rosetta Code is a programming chrestomathy site 😀." |
|||
00000000 082 111 115 101 116 116 097 032 067 111 |Rosetta Co| |
|||
0000000a 100 101 032 105 115 032 097 032 112 114 |de is a pr| |
|||
00000014 111 103 114 097 109 109 105 110 103 032 |ogramming | |
|||
0000001e 099 104 114 101 115 116 111 109 097 116 |chrestomat| |
|||
00000028 104 121 032 115 105 116 101 032 240 159 |hy site ð.| |
|||
00000032 152 128 046 |...| |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |