Does anyone know how to convert a string from ISO-8859-1 to UTF-8 and back in Java?
I'm getting a string from the web and saving it in the RMS (J2ME), but I want to preserve the special chars and get the string from the RMS but with the ISO-8859-1 encoding. How do I do this?
Best Answer
In general, you can't do this. UTF-8 is capable of encoding any Unicode code point. ISO-8859-1 can handle only a tiny fraction of them. So, transcoding from ISO-8859-1 to UTF-8 is no problem. Going backwards from UTF-8 to ISO-8859-1 will cause "replacement characters" (�) to appear in your text when unsupported characters are found.
To transcode text:
or
You can exercise more control by using the lower-level
Charset
APIs. For example, you can raise an exception when an un-encodable character is found, or use a different character for replacement text.