Created
July 10, 2010 12:24
-
-
Save pen/470678 to your computer and use it in GitHub Desktop.
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/ruby1.8 -Ku | |
require "iconv" | |
GETA = [0x3013].pack("U") | |
def to_geta(encoding, str, alter = GETA) | |
cd = Iconv.new(encoding, "utf8") | |
chars = [] | |
str.each_char do |char| | |
begin | |
cd.iconv(char) | |
rescue Iconv::IllegalSequence => e | |
char = block_given? ? yield(e.failed) : alter | |
end | |
chars << char | |
end | |
chars.join | |
end | |
test_str = "漢字♥♡あいゔ①②③" | |
puts to_geta("utf8", test_str) | |
puts to_geta("cp932", test_str) | |
puts to_geta("sjis", test_str, "X") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment