byte数组转16进制:
private static final String toHex(byte hash[])
{
if (hash == null)
{
return null;
}
StringBuffer buf = new StringBuffer(hash.length * 2);
int i;
for (i = 0; i < hash.length; i++)
{
if ((hash[i] & 0xff) < 0x10)
{
buf.append("0");
}
buf.append(Long.toString(hash[i] & 0xff, 16).toUpperCase());
}
return buf.toString();
}
16进制转byte数组:
public static byte[] hex2byte(String strhex) {
if (strhex == null) {
return null;
}
int l = strhex.length();
if (l % 2 == 1) {
return null;
}
byte[] b = new byte[l / 2];
for (int i = 0; i != l / 2; i++) {
b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16);
}
return b;
}
这段代码提供了两个Java方法,一个将byte数组转换为16进制字符串(toHex),另一个将16进制字符串转换回byte数组(hex2byte)。在处理二进制数据与十六进制表示之间转换时,这些函数非常有用。
2830

被折叠的 条评论
为什么被折叠?



