Guest User

Untitled

a guest
May 20th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # UziMonkey <uzimonkey@gmail.com>
  3.  
  4. class HexDump < File
  5. alias :old_write :write
  6.  
  7. def write(s)
  8. @bytes ||= 0 # Bytes written
  9. @line ||= 0 # Offset of current line
  10.  
  11. s.each_byte do|b|
  12. new_line if @bytes % 16 == 0
  13.  
  14. write_byte b
  15. write_ascii b
  16. @bytes += 1
  17. end
  18. end
  19.  
  20. protected
  21. def new_line
  22. seek 0, IO::SEEK_END
  23. @line = tell
  24.  
  25. old_write " " * 76
  26. old_write "\n"
  27.  
  28. seek @line, IO::SEEK_SET
  29. old_write "%08x" % @bytes
  30. end
  31.  
  32. # Write a hex byte
  33. def write_byte(b)
  34. seek @line + 8 + 1 + (3 * (@bytes % 16)), IO::SEEK_SET
  35. old_write "%02x" % b
  36. end
  37.  
  38. # Write an ASCII byte
  39. def write_ascii(b)
  40. seek @line + 8 + (16 * 3) + 3 + (@bytes % 16), IO::SEEK_SET
  41. old_write (b.chr =~ /[[:print:]]/).nil? ? '.' : b.chr
  42. end
  43. end
  44.  
  45. h = HexDump.new( 'output.txt', 'w' )
  46. h.write ARGF.read
Add Comment
Please, Sign In to add comment