Convert UTF-16 unicode characters to UTF-8 in java

61,710

Solution 1

try {
    // Convert from Unicode to UTF-8
    String string = "\u003c";
    byte[] utf8 = string.getBytes("UTF-8");

    // Convert from UTF-8 to Unicode
    string = new String(utf8, "UTF-8");
} catch (UnsupportedEncodingException e) {
}

refer http://www.exampledepot.com/egs/java.lang/unicodetoutf8.html

Solution 2

You can try converting the string into a byte array

byte[] utfString = str.getBytes("UTF-8") ;

and convert that back to a string object by specifying the UTF-8 encoding like

str = new String(utfString,"UTF-8") ;
Share:
61,710
Mubbasher Khaliq
Author by

Mubbasher Khaliq

Updated on February 02, 2020

Comments

  • Mubbasher Khaliq
    Mubbasher Khaliq over 4 years

    When I got JSON then there are \u003c and \u003e instead of < and >. I want to convert them back to utf-8 in java. any help will be highly appreciated. Thanks.

  • Mubbasher Khaliq
    Mubbasher Khaliq over 12 years
    I have used this technique also but it is not working. It returns same string which I passes... although it works in test application. Below is what I am using. public static String unicodeToUTF8(String unicodeStr) { // Convert from Unicode to UTF-8 byte[] utf8 = unicodeStr.getBytes("UTF-8"); String UTF8Str=""; UTF8Str = new String(utf8, "UTF-8"); return UTF8Str; }
  • Hemant Metalia
    Hemant Metalia over 12 years
    it works in test application means there is some problem in application code please check twise the function unicodeToUTF8in your application
  • Mubbasher Khaliq
    Mubbasher Khaliq over 12 years
    I have checked this lots of time and on both test and live applications file.encoding is same i.e. cp1252. What would be the possible options?
  • bames53
    bames53 over 12 years
    The reason a test application with String string = "\u003c" works is because \u003c is a compiler escape just like '\n' is a compiler escape. If you want to test JSON input you have to add an additional level of escaping: String string = "\\u003c"; And in order to process these you need a library that handles these escapes for you. Your JSON parser should be able to do this.