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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
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!
This topic is locked and can not be replied to.