Java字节数组转换成十六进制字符串
时间: 2023-07-08 09:56:17 浏览: 148
Java-byte数组转换成16进制字符串
Java中可以使用以下代码将字节数组转换成十六进制字符串:
```
public static String bytesToHex(byte[] bytes) {
char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int i = 0; i < bytes.length; i++) {
int v = bytes[i] & 0xFF;
hexChars[i * 2] = hexArray[v >>> 4];
hexChars[i * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
```
使用方法如下:
```
byte[] bytes = {0x12, 0x34, (byte) 0xAB, (byte) 0xCD};
String hexString = bytesToHex(bytes);
System.out.println(hexString); // output: "1234ABCDEF"
```
阅读全文