Skip to content

Instantly share code, notes, and snippets.

@vdinovi
Created May 10, 2019 21:48
Show Gist options
  • Select an option

  • Save vdinovi/c152b47be0db4e16e80474855b3d3380 to your computer and use it in GitHub Desktop.

Select an option

Save vdinovi/c152b47be0db4e16e80474855b3d3380 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# stupid version of hexdump to just read the first n bytes from stdin
# no args supported
# useful for when hexdump is not available
#
# to print out the first two bytes from a file
# ./ruby_hexdump 2 < some_file
ARGV.count == 1 or raise "Usage: ruby_hexdump <num bytes>"
bytes = ARGV[0].to_i
buffer = []
$stdin.each_byte do |byte|
buffer << ('%02X' % (byte & 0xFF))
break if buffer.count >= bytes
end
buffer.each_slice(16).each_with_index do |hex_row, index|
$stdout.write '%08X ' % (index * 16 % 0xFFFFFFFF)
$stdout.write hex_row.join(' ')
$stdout.write "\n"
end
$stdout.flush
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment