Java - Convert Text to UTF-8 or Unicode

April 2nd, 2008 | by programming |

Converting text to UTF-8 and unicode in Java is very easy, you can use the following block of code:

try
{
        // Converting from Unicode to UTF-8
        String string = “abc\u5639\u563b”;
        byte[] utf8 = string.getBytes(“UTF-8″);

        // Converting from UTF-8 to Unicode
        string = new String(utf8, “UTF-8″);
}
catch (UnsupportedEncodingException e)
{
       System.out.println(e.getMessage());
}

Post a Comment