In Ruby 1.8.7, you can convert like this:
require "kconv"
# line, destination encoding, source encoding
Kconv.kconv(line,NKF::UTF8,NKF::UTF16)
In Ruby 1.8.7, you can convert like this:
require "kconv"
# line, destination encoding, source encoding
Kconv.kconv(line,NKF::UTF8,NKF::UTF16)
Here’s how you can use iconv to convert between encodings:
iconv --from-code=utf-16le --to-code=utf-8 some_file
If you ever receive this exception: SAXException: Invalid byte 2 of 2-byte UTF-8 sequence, then it means that the XML file you’re trying to read wasn’t encoded in UTF-8. If by any chance,you’re creating that XML, make sure you use the proper encoding when writing the file ( in this case UTF-8 ).
Small snippet:
BufferedReader in = new BufferedReader(new InputStreamReader(stream, encoding)); // encoding is a string, like UTF-8