简体   繁体   English

有人能解释从字节数组到十六进制字符串的转换吗?

[英]Can someone explain the conversion from byte array to hex string?

I recently started looking at MD5 hashing (in Java) and while I've found algorithms and methods to help me accomplish that, I'm left wondering how it actually works. 我最近开始研究MD5哈希(用Java),虽然我找到了帮助我实现这一目标的算法和方法,但我还是想知道它是如何工作的。

For one, I found the following from this URL : 首先,我从以下网址找到以下内容:

private static String convertToHex(byte[] data) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < data.length; i++) {
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do {
            if ((0 <= halfbyte) && (halfbyte <= 9))
                buf.append((char) ('0' + halfbyte));
            else
                buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        }
    return buf.toString();
}

I haven't found any need to use bit-shifting in Java so I'm a bit rusty on that. 我没有发现在Java中使用位移的任何需要,所以我对此有点生疏。 Someone kind enough to illustrate (in simple terms) how exactly does the above code does the conversion? 有人足以说明(简单来说)上述代码究竟是如何进行转换的? ">>>"? “>>>”?

I also found other solutions on StackOverflow, such as here and here , which uses BigInteger instead: 我还在StackOverflow上找到了其他解决方案,例如这里这里 ,它使用了BigInteger:

try {
   String s = "TEST STRING";
   MessageDigest md5 = MessageDigest.getInstance("MD5");
   md5.update(s.getBytes(),0,s.length());
   String signature = new BigInteger(1,md5.digest()).toString(16);
   System.out.println("Signature: "+signature);

} catch (final NoSuchAlgorithmException e) {
   e.printStackTrace();
}

Why does that work too, and which way is more efficient? 为什么这样做也有效,哪种方式更有效?

Thanks for your time. 谢谢你的时间。

private static String convertToHex(byte[] data) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < data.length; i++) {

Up till this point ... just basic set up and starting a loop to go through all bytes in the array 到目前为止......只需要进行基本设置并启动一个循环来遍历数组中的所有字节

        int halfbyte = (data[i] >>> 4) & 0x0F;

bytes when converted to hex are two hex digits or 8 binary digits depending on what base you look at it in. The above statement shifts the high 4 bits down (>>> is unsigned right shift) and logical ANDs it with 0000 1111 so that the result is an integer equal to the high 4 bits of the byte (first hex digit). 转换为十六进制时的字节数是两个十六进制数字或8个二进制数字,具体取决于你查看它的基数。上面的语句将高4位向下移位(>>>是无符号右移)和逻辑与0000 1111进行对比,以便结果是一个等于字节高4位的整数(第一个十六进制数字)。

Say 23 was an input, this is 0001 0111 in binary. 说23是输入,这是二进制的0001 0111。 The shift makes and logical AND coverts this to 0000 0001. 移位使得逻辑AND将其转换为0000 0001。

        int two_halfs = 0;
        do {

This just sets up the do/while loop to run twice 这只是设置do / while循环运行两次

            if ((0 <= halfbyte) && (halfbyte <= 9))
                buf.append((char) ('0' + halfbyte));
            else
                buf.append((char) ('a' + (halfbyte - 10)));

Here we're displaying the actual hex digit, basically just using the zero or a character as a starting point and shifting up to the correct character. 这里我们显示实际的十六进制数字,基本上只使用零或一个字符作为起点并转换到正确的字符。 The first if statement covers all the digits 0-9, and the second covers all digits 10-15 (af in hex) 第一个if语句覆盖所有数字0-9,第二个覆盖所有数字10-15(af为十六进制)

Again, using our example 0000 0001 in decimal is equal to 1. We get caught in the upper if block and add 1 to the '0' character to get the character '1', append that to the string and move on. 再次,使用我们的示例0000 0001十进制等于1.我们陷入上面的if块并将'1'加1以获得字符'1',将其附加到字符串并继续前进。

                halfbyte = data[i] & 0x0F;

Now we set up the integer to just equal the low bits from the byte and repeat. 现在我们将整数设置为恰好等于字节中的低位并重复。

Again, if our input was 23 ... 0001 0111 after the logical AND becomes just 0000 0111 which is 7 in decimal. 同样,如果我们的输入是23 ... 0001 0111,则逻辑AND变为0000 0111,即十进制7。 Repeat the same logic as above and the character '7' is displayed. 重复上述相同的逻辑,显示字符“7”。

            } while(two_halfs++ < 1);

Now we just move on to the next byte in the array and repeat. 现在我们继续前进到数组中的下一个字节并重复。

        }
    return buf.toString();
}

To answer your next question, the Java API already has a base conversion utility built in to BigInteger already. 为了回答您的下一个问题,Java API已经有一个内置于BigInteger的基本转换实用程序。 See the toString(int radix) documentation. 请参阅toString(int radix)文档。

Not knowing the implementation used by the Java API, I can't say for sure, but I'd be willing to bet that the Java implenentation is more efficient than the first somewhat simple algorithm you posted. 不知道Java API使用的实现,我不能肯定地说,但我愿意打赌Java实现比你发布的第一个有点简单的算法更有效。

To answer this bit: 要回答这个问题:

Why does that work too 为什么这也有效

It doesn't. 它没有。 At least, not the same way that the loop version does. 至少,与循环版本不同。 new BigInteger(...).toString(16) will not show leading zeroes, which the former version will. new BigInteger(...)。toString(16)将不会显示前一版本的前导零。 Usually for something like writing out a byte array (especially one representing something like a hash) you would want a fixed-length output so if you want to use that version you'd have to pad it out appropriately. 通常对于写出一个字节数组(特别是一个代表哈希的东西),你会想要一个固定长度的输出,所以如果你想使用那个版本,你必须适当地填充它。

For a thorough explanation on bitshifting check out the answers in the following SO question What are bitwise shift (bit-shift) operators and how do they work? 有关位移的详细说明,请查看以下SO问题中的答案。 什么是按位移位(位移)运算符以及它们如何工作?

He seems to try to convert one single byte into a number smaller than 16, by doing so he can easily determine wich caracther that byte represents with the code 他似乎试图将一个单字节转换为一个小于16的数字,这样他就可以很容易地确定该字节用代码表示的那个字符。

  if ((0 <= halfbyte) && (halfbyte <= 9))
                buf.append((char) ('0' + halfbyte));
            else
                buf.append((char) ('a' + (halfbyte - 10)));

This is a simplistic answer, but im not that bright anyhow =D 这是一个简单的答案,但我不是那么明亮无论如何= D.

These stuff you don't have to write by yourself, because it is already written in apache-commons-codec: 这些东西你不必自己编写,因为它已经用apache-commons-codec编写了:

import org.apache.commons.codec.binary.Hex;
...
Hex.encodeHexString(byte[] array)

There are a lot of more useful methods in Hex class. Hex类中有许多更有用的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM