Skip to content

Instantly share code, notes, and snippets.

@RSully
Created March 3, 2012 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RSully/1963756 to your computer and use it in GitHub Desktop.
Save RSully/1963756 to your computer and use it in GitHub Desktop.
Byte frequency counter in ruby
if ARGV.count != 1
puts "Usage: [command ...] <file>"
exit
end
f = File.new(ARGV[0], 'r')
hash = Hash.new
f.each_byte do |byte|
if !hash[byte] then hash[byte] = 1
else hash[byte] += 1
end
end
#hash.each { |key, obj|
# puts "#{key} ('#{key.chr}')\t\t=#{obj}"
#}
# extra stuffs:
arr = hash.sort_by { |k,v| v }
i = 0
while i <= 255
f = File.new(ARGV[0], 'r')
f.each_byte do |byte|
print (byte^i).chr
end
puts "\n\n"
puts "-----------------------------------"
puts "\n\n"
i += 1
end
# arr.each { |obj|
# a = ''
# b = ''
# obj.each { |obj2|
# if a == ''
# a = obj2
# else
# b = obj2
# end
# }
#
# c = a ^ 0xD
# # i = 0
# # while i <= 255
# # c = a ^ i
# # puts "#{i}\t#{c}\t#{c.chr}\t\t#{b}"
# # i += 1
# # end
# # sleep 0.1
# #
# # puts "------------------------"
#
# puts "#{a}\t('#{a.chr}')\t\t#{b}"
# puts "\t#{c}\t('#{c.chr})\t\t#{b}"
# }
@RSully
Copy link
Author

RSully commented Mar 3, 2012

Authored by Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment