Skip to content

Instantly share code, notes, and snippets.

@henryyan
Created August 30, 2013 16:11
Show Gist options
  • Save henryyan/6391482 to your computer and use it in GitHub Desktop.
Save henryyan/6391482 to your computer and use it in GitHub Desktop.
转换16禁止的Unicode编码为UTF-8中文
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import org.apache.commons.lang.StringUtils;
/**
* @author: Henry Yan
*/
public class Convert16UnicodeToUTF8 {
public static void main(String[] args) throws UnsupportedEncodingException {
Scanner sc = new Scanner(System.in);
System.out.println("输入16进制Unicode编码回车即可:");
while(sc.hasNextLine()) {
String source = sc.nextLine();
String newSource = "";
String result = "";
String[] split = source.split("\\\\x");
for (String s : split) {
if (StringUtils.isBlank(s)){
continue;
}
String s1 = "\\x" + s.substring(0, 2);
newSource += s1;
if (s.length() > 2) {
byte[] byteArr = getBytes(newSource);
result += new String(byteArr, "UTF-8");
result += s.substring(2);
newSource = "";
}
}
byte[] byteArr = getBytes(newSource);
result += new String(byteArr, "UTF-8");
System.out.println(result);
}
}
private static byte[] getBytes(String newSource) {
String sourceArr[] = newSource.split("\\\\"); // 分割拿到形如 xE9 的16进制数据
byte[] byteArr = new byte[sourceArr.length - 1];
for (int i = 1; i < sourceArr.length; i++) {
Integer hexInt = Integer.decode("0" + sourceArr[i]);
byteArr[i - 1] = hexInt.byteValue();
}
return byteArr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment