当前位置: 代码迷 >> J2SE >> Java 编码有关问题
  详细解决方案

Java 编码有关问题

热度:50   发布时间:2016-04-24 12:26:03.0
Java 编码问题
有木有Java大虾啊 我想问一个问题, 就是 e4bb8ae5a4a9e698afe4b8aae5a5bde697a5e5ad90efbc8ce68891e58ebbe8b5b6e99b86e4ba86efbc81efbc81efbc81 这是一个“今天是个好日子,我去赶集了!!!”这句话的utf-8编码转换成的字符串,怎么将这一串字符串转化成原文呢!!!请赐教。。

------解决方案--------------------

public class TestUtfC {
public static byte[] hexToBytes(char[] hex) {
int length = hex.length / 2;
byte[] raw = new byte[length];
for (int i = 0; i < length; i++) {
int high = Character.digit(hex[i * 2], 16);
int low = Character.digit(hex[i * 2 + 1], 16);
int value = (high << 4) | low;
if (value > 127)
value -= 256;
raw[i] = (byte) value;
}
return raw;
}
public static byte[] hexToBytes(String hex) {
return hexToBytes(hex.toCharArray());
}
public static void main(String[] args) {
string x="你上面的";
byte [] b=hexToBytes(x);
try {
System.out.println(new String(b, "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
}

}

------解决方案--------------------
Java code
        String src = "e4bb8ae5a4a9e698afe4b8aae5a5bde697a5e5ad90efbc8ce68891e58ebbe8b5b6e99b86e4ba86efbc81efbc81efbc81";        byte[] bytes = new byte[src.length() / 2];        for (int i = 0; i < bytes.length; i++) {            bytes[i] = (byte)Integer.parseInt(src.substring(2 * i , 2 * i + 2),16);        }        String s = new String(bytes,"utf-8");        System.out.println(s);
  相关解决方案