Rebol3 Code Examplex
Hex dump
A hexadecimal view of binary data, usually shown with addresses, hex bytes, and an ASCII preview.
Rebol [
title: "Rosetta code: Hex dump"
file: %Hex_dump.r3
url: https://rosettacode.org/wiki/Hex_dump
]
hex-dump: function/with [
"Print a binary value as a formatted hex dump."
data [any-string! binary!]
/part length [integer!] "Number of bytes to dump"
/xxd "Use xxd binary-mode output (8-bit groups, 6 bytes per row)"
][
unless binary? data [data: to binary! data]
offset: indexz? data
if part [data: copy/part data length]
clear out
width: either xxd [6][gap-pos: 9 16]
forskip data width [
;; offset column
emit [pad/with enbase offset 16 -8 #"0" " "]
either xxd [
;; binary mode: 8 bits per byte, space-separated, no mid-gap
repeat i width [
emit [
either byte: data/:i [
either zero? byte ["00000000"][enbase byte 2]
][ " "]
space
]
]
][
;; hex bytes with mid-row gap
repeat i width [
emit [
if i = gap-pos [space]
either byte: data/:i [
either zero? byte ["00"][enbase byte 16]
][ " "]
space
]
]
]
;; ASCII column
clear ascii
repeat i width [
unless byte: data/:i [break]
append ascii either all [byte >= 32 byte <= 126] [to char! byte] [#"."]
]
emit [" |" ascii "|^/"]
offset: offset + width
]
print out
][
out: "" ascii: ""
emit: func[v][append out ajoin v]
]
text: "Rosettacode is a programming crestomathy site 😀."
print [as-yellow "Using input text:" as-green text newline]
data: to binary! text
print as-yellow "Hex dump UTF-16BE, offset 0"
hex-dump iconv/to data 'UTF-8 'UTF-16BE
print as-yellow "Hex dump UTF-16LE, offset 0"
hex-dump iconv/to data 'UTF-8 'UTF-16LE
print as-yellow "Hex dump UTF-8, offset 17"
hex-dump at data 17
print as-yellow "Hex dump UTF-8, offset 18, part 11"
hex-dump/part at data 18 11
print as-yellow "Binary dump UTF-8, offset 17, part 24"
hex-dump/part/xxd at data 17 24Output:
Using input text: Rosettacode is a programming crestomathy site 😀.
Hex dump UTF-16BE, offset 0
00000000 00 52 00 6F 00 73 00 65 00 74 00 74 00 61 00 63 |.R.o.s.e.t.t.a.c|
00000010 00 6F 00 64 00 65 00 20 00 69 00 73 00 20 00 61 |.o.d.e. .i.s. .a|
00000020 00 20 00 70 00 72 00 6F 00 67 00 72 00 61 00 6D |. .p.r.o.g.r.a.m|
00000030 00 6D 00 69 00 6E 00 67 00 20 00 63 00 72 00 65 |.m.i.n.g. .c.r.e|
00000040 00 73 00 74 00 6F 00 6D 00 61 00 74 00 68 00 79 |.s.t.o.m.a.t.h.y|
00000050 00 20 00 73 00 69 00 74 00 65 00 20 D8 3D DE 00 |. .s.i.t.e. .=..|
00000060 00 2E |..|
Hex dump UTF-16LE, offset 0
00000000 52 00 6F 00 73 00 65 00 74 00 74 00 61 00 63 00 |R.o.s.e.t.t.a.c.|
00000010 6F 00 64 00 65 00 20 00 69 00 73 00 20 00 61 00 |o.d.e. .i.s. .a.|
00000020 20 00 70 00 72 00 6F 00 67 00 72 00 61 00 6D 00 | .p.r.o.g.r.a.m.|
00000030 6D 00 69 00 6E 00 67 00 20 00 63 00 72 00 65 00 |m.i.n.g. .c.r.e.|
00000040 73 00 74 00 6F 00 6D 00 61 00 74 00 68 00 79 00 |s.t.o.m.a.t.h.y.|
00000050 20 00 73 00 69 00 74 00 65 00 20 00 3D D8 00 DE | .s.i.t.e. .=...|
00000060 2E 00 |..|
Hex dump UTF-8, offset 17
00000010 20 70 72 6F 67 72 61 6D 6D 69 6E 67 20 63 72 65 | programming cre|
00000020 73 74 6F 6D 61 74 68 79 20 73 69 74 65 20 F0 9F |stomathy site ..|
00000030 98 80 2E |...|
Hex dump UTF-8, offset 18, part 11
00000011 70 72 6F 67 72 61 6D 6D 69 6E 67 |programming|
Binary dump UTF-8, offset 17, part 24
00000010 00100000 01110000 01110010 01101111 01100111 01110010 | progr|
00000016 01100001 01101101 01101101 01101001 01101110 01100111 |amming|
0000001C 00100000 01100011 01110010 01100101 01110011 01110100 | crest|
00000022 01101111 01101101 01100001 01110100 01101000 01111001 |omathy|