Class: Hexify

Inherits:
Object
  • Object
show all
Defined in:
lib/hexify.rb

Class Method Summary collapse

Class Method Details

.listing(data, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/hexify.rb', line 3

def self.listing(data, options={})
  text = " "
  raw(data).each_with_index do |byte, i|
    if i != 0 and i%16 == 0
      puts text # takes care of \n
      text = " "
    end
    print "%08x: " % [i] if i == 0 or i%16 == 0
    print "#{byte} "
    text += "%s" % [ byte.hex.chr.gsub(/[^[:print:]]/,'.') ]
  end
end

.raw(data) ⇒ Object



16
17
18
# File 'lib/hexify.rb', line 16

def self.raw(data)
  data.bytes.each.inject([]) { |output, byte| output << "%02x" % [byte.ord] }
end