Created
November 4, 2016 09:31
-
-
Save leikind/0902e8f28654bd36902d6bbff8fd3ec3 to your computer and use it in GitHub Desktop.
pack a UTF8 string in a huge number, and unpack it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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