Created
September 17, 2014 06:59
-
-
Save shaobin0604/4408cbd9b2e88ee18dac to your computer and use it in GitHub Desktop.
GetUnicode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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