Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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