how to convert iso-8859-1 to utf-8 in java

how to convert iso-8859-1 to utf-8 in java

To convert an ISO-8859-1 encoded string to UTF-8 in Java, you can use the getBytes method of the String class to convert the string to a byte array, and then use the constructor method of the String class to create a new string from the byte array, specifying the UTF-8 encoding as an argument.

Here is an example of how you can convert an ISO-8859-1 encoded string to UTF-8 in Java:

‮er‬fer to:lautturi.com
import java.nio.charset.StandardCharsets;

String iso8859 = "This is an ISO-8859-1 encoded string.";
byte[] utf8Bytes = iso8859.getBytes(StandardCharsets.ISO_8859_1);
String utf8 = new String(utf8Bytes, StandardCharsets.UTF_8);

This code defines a string called iso8859 and assigns it an ISO-8859-1 encoded string. It then uses the getBytes method of the String class to convert the string to a byte array, specifying the ISO-8859-1 encoding as an argument. The resulting byte array is stored in a byte array called utf8Bytes.

Finally, the code uses the constructor method of the String class to create a new string called utf8 from the utf8Bytes byte array, specifying the UTF-8 encoding as an argument.

After this code is executed, the utf8 string will contain the same content as the iso8859 string, but it will be encoded in UTF-8 instead of ISO-8859-1.

Created Time:2017-11-01 12:05:15  Author:lautturi