Created
August 22, 2015 08:47
-
-
Save Moligaloo/f4257c25f1e4a9fa946e to your computer and use it in GitHub Desktop.
Convert two byte Unicode to UTF-8
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
#!/usr/bin/env ruby | |
# https://en.wikipedia.org/wiki/UTF-8 | |
def unicode2utf8(str) | |
num = str.to_i(16) | |
c1 = 0xE0 + ((num & 0xF000) >> 12) | |
c2 = 0b10000000 + ((num & 0x0FC0) >> 6) | |
c3 = 0b10000000 + (num & 0x003F) | |
[c1, c2, c3].map{|c| "\\x" + c.to_s(16).upcase }.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment