java将\xE6\x9F\xA5\xE7\x9C\x8B\xE8\xAE\xA2\xE5\x8D\x95转中文

问题:
首先这是一串16进制的字节码,我们的任务就是将十六进制的字节码转为字符串
问题解决:直接上代码
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Author wenBin
 * Date 2019/5/23 9:33
 * Version 1.0
 */
public class asciitochiness {

    public static void main(String[] args) throws UnsupportedEncodingException {

        String str = "\\xE6\\x9F\\xA5\\xE7\\x9C\\x8B\\xE8\\xAE\\xA2\\xE5\\x8D\\x95";
        //注意:在16进制转字符串时我们要先将\x去掉再进行转码
        String stringss = hexStringToString(str.replaceAll("\\\\x", ""));
        System.out.println(stringss);

    }

    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
}

你可能感兴趣的:(LangCode)