Take the 2-minute tour ×

The UTF-8 of "龅" is E9BE85 and the unicode is U+9F85. Following code did not work as expected:

irb(main):004:0> "龅"
=> "\351\276\205"
irb(main):005:0> Iconv.iconv("unicode","utf-8","龅").to_s
=> "\377\376\205\237"

P.S: I am using Ruby1.8.7.

share|improve this question
Version of ruby is important for unicode questions as 1.9 has much better tools. –  Michael Papile Feb 11 '11 at 5:15

3 Answers 3

Ruby 1.9+ is much better equipped to deal with Unicode than 1.8.7, so, I strongly suggest running under 1.9.2 if at all possible.

Part of the problem is that 1.8 didn't understand that a UTF-8 or Unicode character could be more than one byte long. 1.9 does understand that and introduces things like String#each_char.

require 'iconv'

# encoding: UTF-8

RUBY_VERSION # => "1.9.2"
"龅".encoding # => #<Encoding:UTF-8>
"龅".each_char.entries # => ["龅"]
Iconv.iconv("unicode","utf-8","龅").to_s # => 

# ~> -:8:in `iconv': invalid encoding ("unicode", "utf-8") (Iconv::InvalidEncoding)
# ~>    from -:8:in `<main>'

To get the list of available encodings with Iconv, do:

require 'iconv'
puts Iconv.list

It's a long list so I won't add it here.

share|improve this answer
up vote 3 down vote accepted

Should use UNICODEBING// as the target encoding

irb(main):014:0> Iconv.iconv("UNICODEBIG//","utf-8","龅")[0].each_byte {|b| puts b.to_s(16)}
=> "\237\205"
share|improve this answer

You can try this:

"%04x" % "龅".unpack("U*")[0]

=> "9f85"

share|improve this answer

Your Answer


