pierrotlefou
pierrotlefou

Reputation: 40751

convert utf-8 to unicode in ruby

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.

Upvotes: 3

Views: 8282

Answers (3)

pierrotlefou
pierrotlefou

Reputation: 40751

Should use UNICODEBIG// as the target encoding

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

Upvotes: 3

Edson Lima
Edson Lima

Reputation: 41

You can try this:

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

=> "9f85"

Upvotes: 4

the Tin Man
the Tin Man

Reputation: 160571

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.

Upvotes: 4

Related Questions