Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # UziMonkey <uzimonkey@gmail.com>
- class HexDump < File
- alias :old_write :write
- def write(s)
- @bytes ||= 0 # Bytes written
- @line ||= 0 # Offset of current line
- s.each_byte do|b|
- new_line if @bytes % 16 == 0
- write_byte b
- write_ascii b
- @bytes += 1
- end
- end
- protected
- def new_line
- seek 0, IO::SEEK_END
- @line = tell
- old_write " " * 76
- old_write "\n"
- seek @line, IO::SEEK_SET
- old_write "%08x" % @bytes
- end
- # Write a hex byte
- def write_byte(b)
- seek @line + 8 + 1 + (3 * (@bytes % 16)), IO::SEEK_SET
- old_write "%02x" % b
- end
- # Write an ASCII byte
- def write_ascii(b)
- seek @line + 8 + (16 * 3) + 3 + (@bytes % 16), IO::SEEK_SET
- old_write (b.chr =~ /[[:print:]]/).nil? ? '.' : b.chr
- end
- end
- h = HexDump.new( 'output.txt', 'w' )
- h.write ARGF.read
Add Comment
Please, Sign In to add comment