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);
}
}
是这样的,我现在把16进制字符串303038393434AC调hexStringToBytes得到byte数组结果为
[48, 48, 56, 57, 52, 52, -1]
可是最后打出的结果为 008944?
请问下有什么方法能让-1转成正常的String,请指教!
Java
------解决方案--------------------
AC=172
这个 编码 既不在ASCII字符编码中,也不在GB2312字符集编码里,你让人家Java怎么显示??
非要显示的话,改成
String str=new String(aa,"ISO-8859-1");
你会得到一个 ?