Umar Mansuri
Umar Mansuri

Reputation: 127

Converting Unicode to string and String to Unicode in Java

How can I convert this string: This string contains the unicode character unicodetoString("\u0064") and string to unicode like stringtounicode("a")?

public static String GetUnicode(String str_code) {          
String hexCode = Integer.toHexString(str_code.codePointAt(0)).toUpperCase();        
    String hexCodeWithAllLeadingZeros = "0000" + hexCode;
    String hexCodeWithLeadingZeros = hexCodeWithAllLeadingZeros
    .substring(hexCodeWithAllLeadingZeros.length() - 4);
    hexCodeWithLeadingZeros = "\\u" + hexCodeWithLeadingZeros;
    System.out.println("Unicode " + hexCodeWithLeadingZeros);
    return hexCodeWithLeadingZeros;
}    

Upvotes: 2

Views: 9692

Answers (1)

kAnNaN
kAnNaN

Reputation: 3679

To Convert a unicode to string use

StringEscapeUtils.unescapeJava(unicodeString)

Upvotes: 1

Related Questions