Convert non-ASCII chars from ASCII-8BIT to UTF-8

后端 未结 4 1410
鱼传尺愫
鱼传尺愫 2021-02-01 12:46

I\'m pulling text from remote sites and trying to load it into a Ruby 1.9/Rails 3 app that uses utf-8 by default.

Here is an example of some offending text:



        
4条回答
  •  一个人的身影
    2021-02-01 13:33

    This works for me:

    #encoding: ASCII-8BIT
    str = "\xC2\xA92011 AACR"
    p str, str.encoding
    #=> "\xC2\xA92011 AACR"
    #=> #
    
    str.force_encoding('UTF-8')
    p str, str.encoding
    #=> "©2011 AACR"
    #=> #
    

提交回复
热议问题