Advertisement
gocha

Simple Hex Viewer (My First Ruby!)

Dec 11th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.75 KB | None | 0 0
  1. # My First Ruby - I LOVE HEX!
  2. # Large file will get slow speed.
  3. # I guess I should use File.open for such a case.
  4.  
  5. if ARGV.length == 0 then
  6.     puts "No file input"
  7.     exit 1
  8. end
  9.  
  10. if File.exist?(ARGV[0]) then
  11.     printf "%s (%d bytes)\n", ARGV[0], File.size(ARGV[0])
  12. else
  13.     STDERR.printf "No such file: %s\n", ARGV[0]
  14.     exit 1
  15. end
  16.  
  17. Align = 16
  18. offset = 0
  19. dump_s = ""
  20. File.binread(ARGV[0]).bytes { |c|
  21.     if offset % Align == 0 then
  22.         printf "%08X: ", offset
  23.     end
  24.  
  25.     printf "%02X", c
  26.  
  27.     offset += 1
  28.     print " "
  29.     if /[[:print:]]/ === c.chr then
  30.         dump_s << c
  31.     else
  32.         dump_s << "."
  33.     end
  34.  
  35.     if offset % 16 == 0 then
  36.         puts " " << dump_s
  37.         dump_s = ""
  38.     end
  39. }
  40. if offset % Align != 0 then
  41.     puts "   " * (Align - (offset % Align)) << " " + dump_s
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement