6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/hexdump.rb', line 6
def encode(str)
offset = 0
result = []
while raw = str.slice(offset, 16) and raw.length > 0
data = ''
for v in raw.unpack('N* a*')
if v.kind_of? Integer
data << sprintf("%08x ", v)
else
v.each_byte {|c| data << sprintf("%02x", c) }
end
end
text = raw.tr("\000-\037\177-\377", ".")
result << sprintf("%08x %-36s %s", offset, data, text)
offset += 16
if /^(#{ Regexp.quote(raw) })+/n =~ str[offset .. -1]
result << sprintf("%08x ...", offset)
offset += $&.length
if offset == str.length
result << sprintf("%08x %-36s %s", offset-16, data, text)
end
end
end
result
end
|