Skip to content

Instantly share code, notes, and snippets.

@leikind
Created November 4, 2016 09:31
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 leikind/0902e8f28654bd36902d6bbff8fd3ec3 to your computer and use it in GitHub Desktop.
Save leikind/0902e8f28654bd36902d6bbff8fd3ec3 to your computer and use it in GitHub Desktop.
pack a UTF8 string in a huge number, and unpack it
def string_to_number(str)
Integer('0b' + str.unpack('B*')[0])
end
def number_to_utf8_string(num)
str = [num.to_s(2)].pack('B*')
str.force_encoding(Encoding::UTF_8)
end
p string_to_number("мудак")
p number_to_utf8_string(string_to_number("мудак"))
p "мудак" == number_to_utf8_string(string_to_number("мудак"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment