ruby `encode': “\xC3” from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)

后端 未结 4 1397
执念已碎
执念已碎 2020-12-05 22:45

Hannibal episodes in tvdb have weird characters in them.

For example:

Œuf

So ruby spits out:

./manifesto.rb:19:in `         


        
4条回答
  •  遥遥无期
    2020-12-05 23:29

    I had the same problems when saving to the database. I'll offer one thing that I use (perhaps, this will help someone).

    if you know that sometimes your text has strange characters, then before saving you can encode your text in some other format, and then decode the text again after it is returned from the database.

    example:

    string = "Œuf"
    

    before save we encode string

    text_to_save = CGI.escape(string)
    

    (character "Œ" encoded in "%C5%92" and other characters remained the same)

    => "%C5%92uf"

    load from database and decode

    CGI.unescape("%C5%92uf")
    

    => "Œuf"

提交回复
热议问题