日期:2014-05-20 浏览次数:20791 次
public class zhuanTest {
public static void main(String[] args) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
byte[] aa = hexStringToBytes("303038393434AC");
String str=new String(aa,"gb2312");
System.out.println(str);
}
/**
* Convert hex string to byte[]
*
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
// hexString = hexString.toUpperCase(); //如果是大写形式
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789abcdef".indexOf(c);
// return (byte) "0123456789ABCDEF".indexOf(c);
}
}