日期:2014-05-20 浏览次数:20924 次
public static void main(String[] args) {
String str = hexString("好啊tianta了");
System.out.println(str);
byte[] b = HexString2Bytes(str);
try {
String s=new String(b,"GB2312");
System.out.println(s);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String hexString(String str) {
String ret = "";
byte[] b;
try {
b = str.getBytes("GB2312");
for (int i = b.length - 1; i >= 0; i--) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return ret;
}
public static byte[] HexString2Bytes(String hexstr) {
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = b.length-1; i >=0; i--) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4)
------解决方案--------------------
parse(c1));
}
return b;