简体   繁体   English

二进制字符串到十六进制字符串java

[英]Binary strings to Hex strings java

I have this code here, which get a plain text, and turns it to a 512-bit binary string. 我这里有这个代码,它获得一个纯文本,并将其转换为512位二进制字符串。 Then, I would like to turn each 32-bit piece of the string to 8-bit of Hex string, but that part gives me a java.lang.NumberFormatException 然后,我想将每个32位的字符串转换为8位的Hex字符串,但那部分给了我一个java.lang.NumberFormatException

// ----- Turning the message to bits
        byte[] binaryS = s.getBytes("UTF-8");
        String mesInBinary = "";
        for (byte b : binaryS) {
            mesInBinary += '0' + Integer.toBinaryString(b);
        }
        // ----- Message padding & Pre-Processing
        // Binary representation of the length of the message in bits
        String mesBitLength = Integer.toBinaryString(mesInBinary.length());
        // We need the size of the message in 64-bits, so we'll
        // append zeros to the binary length of the message so
        // we get 64-bit
        String appendedZeros = "";
        for (int i = 64 - mesBitLength.length() ; i > 0 ; i--)
            appendedZeros += '0';
        // Calculating the k zeros to append to the message after
        // the appended '1'
        int numberOfZeros = (448 - (mesInBinary.length() + 1)) % 512;
        // Append '1' to the message
        mesInBinary += '1';
        // We need a positive k
        while (numberOfZeros < 0)
            numberOfZeros += 512;
        for (int i = 1 ; i <= numberOfZeros ; i++)
            mesInBinary += '0';
        // append the message length in 64-bit format
        mesInBinary += appendedZeros + mesBitLength;
        System.out.println(mesInBinary);
        // ----- Parsing the padded message
        // Breaking the message to 512-bit pieces
        // And each piece, to 16 32-bit word blocks
        String[] chunks = new String[mesInBinary.length() / 512];
        String[] words = new String[64 * chunks.length];
        for (int i = 0 ; i < chunks.length ; i++) {
            chunks[i] = mesInBinary.substring((512 * i), (512 * (i + 1)));
            // Break each chunk to 16 32-bit blocks
            for (int j = 0 ; j < 16 ; j++) {
                words[j] = Long.toHexString(Long.parseLong(chunks[i].substring((32 * j), (32 * (j + 1)))));
            }
        }

The last code line is the problematic one and of which I get the execption. 最后一个代码行是有问题的,我得到了execption。 Any suggestions? 有什么建议么?

The last statement * should specify a radix of 2, I think: 最后一个语句 *应该指定2的基数,我想:

words[j] = Long.toHexString(
    Long.parseLong(chunks[i].substring((32 * j), (32 * (j + 1))), 2));

* Not the last line of code, MДΓΓ :-) *不是最后一行代码,MДΓΓ:-)

From the Long docs : 来自Long文档

public static long parseLong(String s) throws NumberFormatException : public static long parseLong(String s) throws NumberFormatException

Parses the string argument as a signed decimal long. 将字符串参数解析为带符号的十进制长度。 The characters in the string must all be decimal digits... 字符串中的字符必须都是十进制数字......

public static long parseLong(String s, int radix) throws NumberFormatException : public static long parseLong(String s, int radix) throws NumberFormatException

Parses the string argument as a signed long in the radix specified by the second argument. 将字符串参数解析为第二个参数指定基数中的有符号long。 The characters in the string must all be digits of the specified radix... 字符串中的字符必须都是指定基数的数字...

You're calling the first version of Long.parseLong() , which expects a decimal number, not a binary one. 你正在调用Long.parseLong()的第一个版本,它需要一个十进制数,而不是二进制数。 Use the second version with a radix of 2 to indicate binary. 使用基数为2的第二个版本表示二进制。

EDIT: The reason being that a 32-digit decimal number won't fit into a Long , but a binary one will. 编辑:原因是32位十进制数不适合Long ,但二进制数将。

for (int i = 0 ; i < chunks.length ; i++) 
{
     chunks[i] = mesInBinary.substring((512 * i), (512 * (i + 1)));
     // Break each chunk to 16 32-bit blocks
     for (int j = 0 ; j < 16 ; j++) 
     {
         words[j] = Long.toHexString(Long.parseLong(chunks[i].substring((32 * j), (32 * (j + 1))),2));
     }
 }

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

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