Friday, July 6, 2012

How to get unicode characters from String ?


Below is the Example.....

StringBuffer a = new StringBuffer();
try {
String line = "Shailesh Shukla";
for (int index = 0; index < line.length(); index++) {
String hexCode = Integer.toHexString(line.codePointAt(index))
.toUpperCase();
String hexCodeWithAllLeadingZeros = "0000" + hexCode;
String hexCodeWithLeadingZeros = hexCodeWithAllLeadingZeros
.substring(hexCodeWithAllLeadingZeros.length() - 4);
a.append("\\u" + hexCodeWithLeadingZeros + " ");
}
} catch (Exception e) {
}
System.out.println(a.toString());

OUTPUT For String "Shailesh Shukla"

\u0053 \u0068 \u0061 \u0069 \u006C \u0065 \u0073 \u0068 \u0020 \u0053 \u0068 \u0075 \u006B \u006C \u0061 

No comments:

Post a Comment