Skip to content

Instantly share code, notes, and snippets.

@giacomomacri
Created October 9, 2012 12:51
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 giacomomacri/3858626 to your computer and use it in GitHub Desktop.
Save giacomomacri/3858626 to your computer and use it in GitHub Desktop.
Convert from/to ASCII-8 bit
my_string = "jackmacrack"
my_string_ascii = ""
my_string.each_char { |c| my_string_ascii += "#{c.ord}".rjust(3, '0') } #padding to 3 char, otherwise it's impossibile to reconvert
puts my_string_ascii # => 106097099107109097099114097099107
my_original_string = ""
my_string_ascii.scan(/.../).each {|c| my_original_string += c.to_i.chr }
puts my_original_string # => jackmacrack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment