Skip to content

Instantly share code, notes, and snippets.

@jinie
Created July 5, 2011 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jinie/1065394 to your computer and use it in GitHub Desktop.
Save jinie/1065394 to your computer and use it in GitHub Desktop.
Read binary file in ruby and print hex output
#!/usr/bin/ruby
require 'getoptlong'
filename = ""
blocksize = 1024
opts = GetoptLong.new(
[ "-f", "--file", GetoptLong::REQUIRED_ARGUMENT],
[ "-b", "--block", GetoptLong::REQUIRED_ARGUMENT]
)
opts.each{|o,a|
case o
when '-f', "--file"
filename = a
when 'b', '--block'
blocksize = a.to_i
end
}
open(filename,"rb"){|f|
str = ""
while buf = f.read(blocksize)
0.upto(buf.size-1){|i|
str+= "0x"+(buf[i].to_s(16))+" "
}
end
puts str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment