Ruby Ruby 怎么在 汉字 和 Unicode 码之间互相转换

alixiaomiao · June 24, 2015 · Last by alixiaomiao replied at June 24, 2015 · 2125 hits

比如 "饖" => '9956''9956' => "饖"

 "\u{9956}"
=> "饖"

 "饖".ord.to_s(16)
=> "9956"

#1 楼 @42thcoder 第二个我知道了,第一个我说的是 "9956" 不带 "\u" 头的,”9956“ 前不能直接插入 "\u" 符号。 或者这么说吧, str = "9956" ,把 str 转成对应的汉字。

['9956'.hex].pack 'U'
  1. eval("\"\\u{#{str}}\"")

  2. '' << str.hex

  3. [str.hex].pack('U')

#3 楼 @rei 感谢 #4 楼 @lululau 这么多黑科技啊。

You need to Sign in before reply, if you don't have an account, please Sign up first.