How do I take data that might be encoded using latin-1 text format and interpret it as utf-8

Tal Angel :

I have a string that might contain Latin symbols and / or letters.

How do I take that String and convert it to UTF-8 encoded String?

For example if my String is:

"óóó"

I wish to convert it to be:

"óóó"

Ryan :
public static void main(String [] args) {
    String input = "ÁÉÍÓÚÜÑáéíóúüñ¡¿";
    //simulate ISO_8859 input
    ByteBuffer iso = StandardCharsets.ISO_8859_1.encode(input);
    CharBuffer buffer = StandardCharsets.ISO_8859_1.decode(iso);
    ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(buffer);
    System.out.println(new String(byteBuffer.array()));
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=193213&siteId=1