Skip to content

Instantly share code, notes, and snippets.

@pen
Created July 10, 2010 12:24
Show Gist options
  • Save pen/470678 to your computer and use it in GitHub Desktop.
Save pen/470678 to your computer and use it in GitHub Desktop.
#!/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