Back

encoding problem in Ruby (ruby中的汉字转换)

发布时间: 2013-04-16 01:24:00

Today I am face a problem:  how to convert 汉字 to \uxxx\uxxx ? 

after googled and quickly browsed several posts, I found this answer:  

Using active support: 

>> require 'rubygems'
>> require 'active_support'
>> a ='汉字'

>> a.encoding
=> #

>> result = ActiveSupport::JSON.encode(a)
=> "\"\\u6c49\\u5b57\""

>> result.encoding
=> #

It's strange that both of them are encoded in UTF-8. ( I am confused by this UTF8, UTF16, unicode stuffs... @.@ )  有点蒙。。。回头把它给补上。

Back