Convert Windows-1257 to ISO-8859-13 java charset

廉价感情. 提交于 2019-12-24 15:41:09

问题


I have array of bytes that should be converted to string. That array consists of Windows-1257 encoded text. What is the best way of converting this array to string? Later I will need to convert string to ISO-8859-13 byte array, but I know how to make this part of job.

I tried like this:

String result = new String(currentByteArray, "ISO-8859-13");

But of course got garbage in local character places.


回答1:


String unicodeString = new String(currentByteArray, "Windows-1257");
byte[] result = unicodeString.getBytes("ISO-8859-13");

or

PrintWriter out = new PrintWriter(file, "ISO-8859-13");

Java is very simple: String/Reader&Writer is Unicode text capable to contain all characters. And binary byte[]s/InputStream&OutputStream is for binary data.

Hence the String constructor for bytes needs the original encoding of those bytes, and getting bytes needs the encoding where those bytes should be in.

Be aware that there are overloaded versions with one parameter, without encoding. That uses the platform encoding; not-portable.



来源:https://stackoverflow.com/questions/19066042/convert-windows-1257-to-iso-8859-13-java-charset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!