Forum: Ruby How to resolve mixed character Encodings in ruby?

7f3c5d65bbde886a5f6e10364a8aa130?d=identicon&s=25 ms z (Guest)
on 2013-11-06 08:30
(Received via mailing list)
I'm learning ruby and try to get the filename from a ftp server. The
string
I got was encoded in *gb2312(simplified Chinese)*, It's success in most
cases with these codes:

str = str.force_encoding("gb2312")
str = str.encode("utf-8")

but it will make an error "in encode': "\xFD" followed by "\x88" on
GB2312
(Encoding::InvalidByteSequenceError)" if the string contains the symbol
"["
 or "【".

Then I use

force_encoding("gb2312").valid_encoding?

to detect if all filename strings were encoded by gb2312, some of them
return false. I think I must deal with a string list that's in mixed
character encodings. Help me! Thank you!
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.