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(); } }}
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);
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();
}
}}
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);
String x="e4bb8ae5a4a9e698afe4b8aae5a5bde697a5e5ad90efbc8ce68891e58ebbe8b5b6e99b86e4ba86efbc81efbc81efbc81";
说我有非法字符,不能回复