java中将字节数组转换成16进制的字符串
时间: 2023-07-08 08:33:27 浏览: 198
可以使用Java自带的BigInteger类的toString方法来将字节数组转换成16进制的字符串,示例代码如下:
```
import java.math.BigInteger;
public class ByteArrayToHex {
public static String byteArrayToHex(byte[] bytes) {
BigInteger bigInt = new BigInteger(1, bytes);
String hex = bigInt.toString(16);
int paddingLength = (bytes.length * 2) - hex.length();
if (paddingLength > 0) {
return String.format("%0" + paddingLength + "d", 0) + hex;
} else {
return hex;
}
}
public static void main(String[] args) {
byte[] bytes = {0x12, 0x34, (byte) 0xAB, (byte) 0xCD};
String hex = byteArrayToHex(bytes);
System.out.println(hex);
}
}
```
以上代码执行的结果为:
```
1234abcd
```
阅读全文