16进制字符串转为16进制byte[]
java16进制字符串转为16进制byte[]
方法一
/** * 16进制字符串转为16进制 * @param hex 16进制的字符串 * @return */ public static byte[] hexString2Bytes(String hex) { if ((hex == null) || (hex.equals(""))){ return null; } else if (hex.length()%2 != 0){ return null; } else{ hex = hex.toUpperCase(); int len = hex.length()/2; byte[] b = new byte[len]; char[] hc = hex.toCharArray(); for (int i=0; i<len; i++){ int p=2*i; b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p+1])); } return b; } }
方法二
byte[] decodedHex = DatatypeConverter.parseHexBinary(str);