Wednesday 14 March 2018

String encoding issue

Sometimes we may get a scenario where string need to be decoded. We may get request in correct format, but we need to make sure its correctly decoded.

package com.kishore.encode;

import java.nio.charset.StandardCharsets;

public class CharsetEncoding {
    public static void main(String[] args) {
        String name = "(Kishore PROĆ¢„¢)";
        byte[] bytes = name.getBytes(StandardCharsets.ISO_8859_1);
        name = new String(bytes, StandardCharsets.UTF_8);
        System.out.println("name ::" + name);
    }
}

Output: Kishore PRO™