public class HexUtil {
private final static String HEX = "0123456789abcdef";
/**
* 16进制字符串转字节数组
*
* @param hexString 16进制字符串
* @return 转化后的字节数组
*/
public static byte[] hexToByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++) {
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
}
return result;
}
/**
* 字节数组转16进制字符串
*
* @param bytes 字节数组
* @return 转化后的字符串
*/
public static String byteToHex(byte[] bytes) {
if (bytes == null) {
return "";
}
StringBuilder result = new StringBuilder(2 * bytes.length);
for (byte b : bytes) {
result.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
return result.toString();
}
}
Byte数组与16进制字符串相互转换
最新推荐文章于 2024-08-12 14:15:44 发布