Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created September 17, 2014 06:59
Show Gist options
  • Save shaobin0604/4408cbd9b2e88ee18dac to your computer and use it in GitHub Desktop.
Save shaobin0604/4408cbd9b2e88ee18dac to your computer and use it in GitHub Desktop.
GetUnicode.java
static String getUnicode(String s) {
try {
StringBuffer out = new StringBuffer("");
byte[] bytes = s.getBytes("unicode");
for (int i = 0; i < bytes.length - 1; i += 2) {
out.append("\\u");
String str = Integer.toHexString(bytes[i + 1] & 0xff);
for (int j = str.length(); j < 2; j++) {
out.append("0");
}
String str1 = Integer.toHexString(bytes[i] & 0xff);
out.append(str1);
out.append(str);
}
return out.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment