用ruby语言如何从文本文件取得文字列后,想显示得到文字列中每一个字符的Unicode值?

来源:百度知道 编辑:UC知道 时间:2024/06/10 08:49:55

在命令行安装unicode包:
gem install unicode

在程序里:
require 'unicode'

$KCODE = 'UTF-8' # only used when encoding is not specified.

u = "\352\260\200\353\202\230\353\213\244".to_u
u.length #=> 3
u.to_a #=> [44032, 45208, 45796], array of codepoint
u.inspect #=> <U+AC00><U+B098><U+B2E4>
u.to_s #=> "\352\260\200\353\202\230\353\213\244"
u.encoding = "EUC-KR"
u.to_s #=> "\260\241\263\252\264\331"
u.to_s("UTF-8") #=> "\352\260\200\353\202\230\353\213\244"
u.length #=> 3
u = "\260\241\263\252\264\331".to_u("EUC-KR")
u.inspect #=> <U+AC0