JAVA
程序员文章站
2024-02-28 09:12:28
...
/**
* 从字节数组转换到16进制字符串
* @param b 字节数组
* @return 转换后的字符串
*/
public static String hexStringFromByteArray(byte[] b)
{
if (b == null)
{
return ""; //$NON-NLS-1$
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++)
{
String s = Integer.toHexString(b[i]).toUpperCase(Locale.getDefault());
if (s.length() < 2)
{
sb.append('0');
sb.append(s);
sb.append(' ');
}
else if (s.length() > 2)
{
sb.append(s.substring(s.length() - 2));
sb.append(' ');
}
else
{
sb.append(s);
sb.append(' ');
}
}
return sb.toString();
}