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

发布于 2024-12-22 22:08:40 字数 2098 浏览 2 评论 0原文

我这里有这段代码,它获取纯文本,并将其转换为 512 位二进制字符串。 然后,我想将字符串的每个 32 位片段转换为 8 位十六进制字符串,但这部分给了我一个 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)))));
            }
        }

最后一行代码是有问题的,我得到了其中的例外。有什么建议吗?

I have this code here, which get a plain text, and turns it to a 512-bit binary string.
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

// ----- 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. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

另类 2024-12-29 22:08:40

最后一个语句*应该指定基数2,我认为:

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

*不是最后一行代码,MДДГИ:-)

The last statement* should specify a radix of 2, I think:

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

*Not the last line of code, MДΓΓ :-)

森罗 2024-12-29 22:08:40

来自 Long 文档:

public static long parseLong(String s) 抛出 NumberFormatException:

将字符串参数解析为有符号十进制长整型。字符串中的字符必须全部是十进制数字...

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

将字符串参数解析为第二个参数指定的基数中的有符号长整型。字符串中的字符必须都是指定基数的数字...

您正在调用 Long.parseLong() 的第一个版本,它需要一个十进制数字,不是二进制的。使用基数为 2 的第二个版本来表示二进制。

编辑:原因是 32 位十进制数字不适合 Long,但二进制数字可以。

From the Long docs:

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:

Parses the string argument as a signed long in the radix specified by the second argument. 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. Use the second version with a radix of 2 to indicate binary.

EDIT: The reason being that a 32-digit decimal number won't fit into a Long, but a binary one will.

水水月牙 2024-12-29 22:08:40
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));
     }
 }
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));
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文