//十六进制字符串转字节数组
public static byte[] fromHexStringToBytes(String hexString) {
if ((hexString == null) || (hexString.equals("")) || hexString.length()%2 != 0){
return null;
}else{
hexString = hexString.toUpperCase();
int length = hexString.length()/2;
char[] bytec = hexString.toCharArray();
byte[] bit = new byte[length];
for (int i = 0; i < length; i++){
int p = 2 * i;
//两个十六进制字符转换成1个字节,第1个字符转换成byte后左移4位,然后和第2个字符的byte做或运算
bit[i] = (byte) (fromCharToByte(bytec[p]) << 4 | fromCharToByte(bytec[p + 1]));
}
return bit;
}
}
//字符转换为字节
private static byte fromCharToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
Java十六进制字符串转byte和字符转字节
最新推荐文章于 2024-07-14 03:06:37 发布