Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 649
Mubbasher Khaliq Avatar asked Jan 31 '12 06:01

Mubbasher Khaliq


People also ask

Does Java use UTF-8 or UTF-16?

The native character encoding of the Java programming language is UTF-16.

What is uFFFF in Java?

String str2 is assigned \uFFFF which is the highest value in Unicode. To convert them into UTF-8, we use the getBytes(“UTF-8”) method.

What is StandardCharsets UTF_8 in Java?

Introduction. When working with Strings in Java, we oftentimes need to encode them to a specific charset, such as UTF-8. UTF-8 represents a variable-width character encoding that uses between one and four eight-bit bytes to represent all valid Unicode code points.


2 Answers

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

like image 89
Hemant Metalia Avatar answered Sep 18 '22 08:09

Hemant Metalia


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") ;
like image 44
Rocky Avatar answered Sep 21 '22 08:09

Rocky