Created
February 6, 2013 14:02
-
-
Save charlie/4722676 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.9.3p374 :001 > "\x90".force_encoding(Encoding::WINDOWS_1250).valid_encoding? | |
=> true | |
1.9.3p374 :002 > "\x90".force_encoding(Encoding::WINDOWS_1250).encode("UTF-8") | |
Encoding::UndefinedConversionError: "\x90" to UTF-8 in conversion from Windows-1250 to UTF-8 | |
from (irb):2:in `encode' | |
from (irb):2 | |
from /Users/charleshornberger/.rvm/rubies/ruby-1.9.3-p374/bin/irb:16:in `<main>' |
Wait, I figured it out. You need to pass the :undef
option:
x = "\x90"
x.force_encoding(Encoding::WINDOWS_1250)
x.valid_encoding?
puts x.encode("UTF-8", :invalid => :replace, :undef => :replace)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Supposedly you can pass options to
encode
for what to do in the case of bytes that don't translate: