简体   繁体   English

使用ruby将unicode转换为字符

[英]convert unicode into character with ruby

I found a dictionary of Chinese characters in unicode. 我在unicode中找到了一个汉字字典。 I'm trying to build a database of Characters out of this dictionary but I don't know how to convert unicode to a character.. 我正在尝试从这个字典中构建一个Character数据库,但我不知道如何将unicode转换为一个字符..

p "国".unpack("U*").first #this gives the unicode 22269

How can convert 22269 back into the character value which would be the opposite of the line above. 如何将22269转换回与上面一行相反的字符值。

Ruby 1.9 : Ruby 1.9:

p "国".codepoints.first #=> 22269
p 22269.chr('UTF-8') #=> "国"
[22269].pack('U*') #=> "国" or "\345\233\275"

Edit : Works in 1.8.6+ (verified in 1.8.6, 1.8.7, and 1.9.2). 编辑 :适用于1.8.6+(在1.8.6,1.8.7和1.9.2中验证)。 In 1.8.x you get a three-byte string representing the single Unicode character, but using puts on that causes the correct Chinese character to appear in the terminal. 在1.8.x中,您将获得一个表示单个Unicode字符的三字节字符串,但使用puts会导致正确的中文字符出现在终端中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM