[REBOL] dump-binary
From: jvargas::whywire::net at: 30-Apr-2004 12:04
I have being working with binaries a lot lately so I created this
tool that lets you examine the contents of a binary in a more
explicit way that you using probe.
Please critique my code so that I can improve it.
Thank you, and enjoy -- Jaime
dump-binary: func ['buffer /local v b t c r pad pl len chars tmp][
chars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9" #"-"]
either binary! = type? b: get buffer [
t: to-integer divide len: length? b 16
r: remainder len 16
pl: either odd? r [40 - (5 * (r - 1) / 2)][40 - (5 * r / 2)]
pad: head insert/dup copy {} #" " pl
print [buffer "=="]
for j 0 t 1 [
prin head insert/dup tmp: to-string j "0" 4 - length? tmp
prin ":^-"
; first print the hex format
c: either j = t [r][16]
repeat i c [
prin lowercase at to-hex pick b (j * 16 + i) 7
if equal? i // 2 0 [prin " "]
]
if all [r <> 0 j = t] [prin pad]
either j <> t [prin "^-|^- "][if r <> 0 [prin "^-|^- "]]
; now print the ascii character
repeat i c [
prin either find chars v: to-char pick b (j * 16 + i) [v][#"."]
]
prin newline
]
][
make error! reform [buffer "must be a binary!"]
]
]