一种简单的方法是检查输出的位数,
Integer.toHexString()并在需要时在每个字节前添加一个前导零。像这样:
public static String toHexString(byte[] bytes) { StringBuilder hexString = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString();}


