使用这个方法可以传进去的
16进制的数字组成的
字符串转化为utf-8格式的字符串
public static String toStringHex1(String s) {
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");// UTF-16le:Not
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
追问
我要ASCII格式的字符串,有吗,谢了。
追答
那就修改s = new String(baKeyword, "ASCII")这一行就行了,后面的表示要转化的编码格式可以选很多种,以下是你要的代码
public static String toStringHex1(String s) {
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, "ASCII");
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}