我有一个字节数组。 我希望将该数组的每个字节String转换为相应的十六进制值。
Java中是否有将字节数组转换为十六进制的函数?
我有一个字节数组。 我希望将该数组的每个字节String转换为相应的十六进制值。
Java中是否有将字节数组转换为十六进制的函数?
当前回答
Java 17:引入Java .util. hexformat
Java 17提供了一个实用程序,可以将字节数组和数字转换为对应的十六进制数。假设我们有一个“Hello World”的MD5摘要作为一个字节数组:
var md5 = MessageDigest.getInstance("md5");
md5.update("Hello world".getBytes(UTF_8));
var digest = md5.digest();
现在我们可以使用HexFormat.of().formatHex(byte[])方法将给定的byte[]转换为它的十六进制形式:
jshell> HexFormat.of().formatHex(digest)
$7 ==> "3e25960a79dbc69b674cd4ec67a72c62"
withUpperCase()方法返回前面输出的大写版本:
jshell> HexFormat.of().withUpperCase().formatHex(digest)
$8 ==> "3E25960A79DBC69B674CD4EC67A72C62"
其他回答
我在这里发帖是因为现有的答案都没有解释为什么他们的方法有效,我认为这对这个问题真的很重要。在某些情况下,这会导致所提出的解决方案显得不必要的复杂和微妙。为了说明这一点,我将提供一个相当简单的方法,但我将提供更多细节来帮助说明为什么它是有效的。
首先,我们要做什么?我们希望将字节值(或字节数组)转换为ASCII中表示十六进制值的字符串。所以第一步是找出Java中的字节是什么:
字节数据类型是一个8位有符号二补整数。最小值为-128,最大值为127(包括)。在大型数组中,字节数据类型对于节省内存非常有用,因为节省内存实际上很重要。它们也可以用来代替int,它们的限制有助于澄清你的代码;变量的范围是有限的这一事实可以作为一种文档形式。
What does this mean? A few things: First and most importantly, it means we are working with 8-bits. So for example we can write the number 2 as 0000 0010. However, since it is two's complement, we write a negative 2 like this: 1111 1110. What is also means is that converting to hex is very straightforward. That is, you simply convert each 4 bit segment directly to hex. Note that to make sense of negative numbers in this scheme you will first need to understand two's complement. If you don't already understand two's complement, you can read an excellent explanation, here: http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
将二的补数转换为十六进制
一旦一个数是二的补数,那么把它转换成十六进制就非常简单了。一般来说,从二进制转换为十六进制是非常简单的,正如您将在接下来的两个示例中看到的,您可以直接从2的补码转换为十六进制。
例子
例1:将2转换为十六进制。
1)首先将2转换为二进制的2的补码:
2 (base 10) = 0000 0010 (base 2)
2)现在将二进制转换为十六进制:
0000 = 0x0 in hex
0010 = 0x2 in hex
therefore 2 = 0000 0010 = 0x02.
例2:将-2(2的补数)转换为十六进制。
1)首先将-2转换为二进制的二元补码:
-2 (base 10) = 0000 0010 (direct conversion to binary)
1111 1101 (invert bits)
1111 1110 (add 1)
therefore: -2 = 1111 1110 (in two's complement)
2)现在转换为十六进制:
1111 = 0xF in hex
1110 = 0xE in hex
therefore: -2 = 1111 1110 = 0xFE.
在Java中执行此操作
现在我们已经介绍了这个概念,您将发现我们可以通过一些简单的屏蔽和移动实现我们想要的效果。要理解的关键是,您试图转换的字节已经是2的补码。你不用自己做这个转换。我认为这是这个问题的一个主要混淆点。以下面的字节数组为例:
byte[] bytes = new byte[]{-2,2};
我们只是手动将它们转换为十六进制,但我们如何在Java中做到这一点呢?方法如下:
步骤1:创建一个StringBuffer来保存我们的计算。
StringBuffer buffer = new StringBuffer();
步骤2:隔离高阶位,将它们转换为十六进制,并将它们附加到缓冲区中
Given the binary number 1111 1110, we can isolate the higher order bits by first shifting them over by 4, and then zeroing out the rest of the number. Logically this is simple, however, the implementation details in Java (and many languages) introduce a wrinkle because of sign extension. Essentially, when you shift a byte value, Java first converts your value to an integer, and then performs sign extension. So while you would expect 1111 1110 >> 4 to be 0000 1111, in reality, in Java it is represented as the two's complement 0xFFFFFFFF!
回到我们的例子:
1111 1110 >> 4 (shift right 4) = 1111 1111 1111 1111 1111 1111 1111 1111 (32 bit sign-extended number in two's complement)
然后我们可以用掩码隔离这些位:
1111 1111 1111 1111 1111 1111 1111 1111 & 0xF = 0000 0000 0000 0000 0000 0000 0000 1111
therefore: 1111 = 0xF in hex.
在Java中,我们可以一次性完成这一切:
Character.forDigit((bytes[0] >> 4) & 0xF, 16);
forDigit函数只是将您传递给它的数字映射到0-F的十六进制数集。
步骤3:接下来我们需要分离低阶位。因为我们想要的比特已经在正确的位置,我们可以把它们屏蔽掉:
1111 1110 & 0xF = 0000 0000 0000 0000 0000 0000 0000 1110 (recall sign extension from before)
therefore: 1110 = 0xE in hex.
像以前一样,在Java中我们可以一次性完成这些:
Character.forDigit((bytes[0] & 0xF), 16);
把这些放在一起,我们可以把它作为一个for循环,并转换整个数组:
for(int i=0; i < bytes.length; i++){
buffer.append(Character.forDigit((bytes[i] >> 4) & 0xF, 16));
buffer.append(Character.forDigit((bytes[i] & 0xF), 16));
}
希望这个解释能让那些想知道在网上找到的许多例子中到底发生了什么的人更清楚。希望我没有犯任何严重的错误,但建议和纠正是非常欢迎的!
如果你想要一个恒定宽度的十六进制表示,即0A而不是a,这样你就可以明确地恢复字节,尝试format():
StringBuilder result = new StringBuilder();
for (byte bb : byteArray) {
result.append(String.format("%02X", bb));
}
return result.toString();
这是一条非常快的路。不需要外部库。
final protected static char[] HEXARRAY = "0123456789abcdef".toCharArray();
public static String encodeHexString( byte[] bytes ) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEXARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEXARRAY[v & 0x0F];
}
return new String(hexChars);
}
试试这个方法:
byte bv = 10;
String hexString = Integer.toHexString(bv);
处理数组(如果我没理解错的话):
byte[] bytes = {9, 10, 11, 15, 16};
StringBuffer result = new StringBuffer();
for (byte b : bytes) {
result.append(String.format("%02X ", b));
result.append(" "); // delimiter
}
return result.toString();
正如polygeneluants所提到的,String.format()是与Integer.toHexString()相比的正确答案(因为它以正确的方式处理负数)。
我不知道你说的字节字符串到底是什么意思,但这里有一些从字节到字符串的转换,反之亦然,当然官方文档上有更多
Integer intValue = 149;
对应的字节值为:
Byte byteValue = intValue.byteValue(); // this will convert the rightmost byte of the intValue to byte, because Byte is an 8 bit object and Integer is at least 16 bit, and it will give you a signed number in this case -107
从Byte变量中返回整数值:
Integer anInt = byteValue.intValue(); // This will convert the byteValue variable to a signed Integer
从字节和整数到十六进制字符串: 我是这样做的:
Integer anInt = 149
Byte aByte = anInt.byteValue();
String hexFromInt = "".format("0x%x", anInt); // This will output 0x95
String hexFromByte = "".format("0x%x", aByte); // This will output 0x95
将字节数组转换为十六进制字符串: 据我所知,没有一个简单的函数可以将某个对象的数组中的所有元素转换为另一个对象的元素,所以你必须自己做。您可以使用以下函数:
从byte[]到String:
public static String byteArrayToHexString(byte[] byteArray){
String hexString = "";
for(int i = 0; i < byteArray.length; i++){
String thisByte = "".format("%x", byteArray[i]);
hexString += thisByte;
}
return hexString;
}
从十六进制字符串到字节[]:
public static byte[] hexStringToByteArray(String hexString){
byte[] bytes = new byte[hexString.length() / 2];
for(int i = 0; i < hexString.length(); i += 2){
String sub = hexString.substring(i, i + 2);
Integer intVal = Integer.parseInt(sub, 16);
bytes[i / 2] = intVal.byteValue();
String hex = "".format("0x%x", bytes[i / 2]);
}
return bytes;
}
现在已经太迟了,但我希望这能帮助到其他人;)