Skip to content

Instantly share code, notes, and snippets.

@elvuel
Forked from jinie/gist:1065394
Created April 19, 2012 09:39
Show Gist options
  • Select an option

  • Save elvuel/2419978 to your computer and use it in GitHub Desktop.

Select an option

Save elvuel/2419978 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