When I received the email, it was sent by iso-2022-jp, so the characters were garbled.
Convert to utf-8 using ruby encode
Generate a string encoded in iso-2022-jp for verification
puts "Hoge".encode('iso-2022-jp')
# => $B$[$2(B
Write encode as follows
str.encode(Conversion destination,Conversion source,Conversion options)
This time I want to convert the character string of iso-2022-jp to utf-8, so it will be as follows
str = "Hoge".encode('iso-2022-jp')
puts str
# => $B$[$2(B
puts str.encode('utf-8', 'iso-2022-jp', invalid: :replace, undef: :replace, replace: '')
This time I specify an option to replace the unconvertible character with an empty string
You can only convert this way if you know the source, It seems that it can be used effectively when the mail is garbled like this time.
https://docs.ruby-lang.org/ja/latest/method/String/i/encode.html
Recommended Posts