【问题标题】:Binary strings to Hex strings java二进制字符串到十六进制字符串java
【发布时间】:2011-12-25 07:32:07
【问题描述】:

我这里有这段代码,它得到一个纯文本,并将其转换为 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)))));
            }
        }

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

【问题讨论】:

  • 最后一行代码是}...
  • 天知道我的意思是 words[j] = Long.toHexString(Long.parseLong(chunks[i].substring((32 * j), (32 * (j + 1))))) ;

标签: java bits sha256


【解决方案1】:

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

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

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

【讨论】:

【解决方案2】:

来自Long docs

public static long parseLong(String s) throws NumberFormatException:

将字符串参数解析为带符号的十进制长。字符串中的字符必须都是十进制数字...

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

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

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

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

【讨论】:

    【解决方案3】:
    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));
         }
     }
    

    【讨论】:

      猜你喜欢
      • 2014-07-15
      • 2012-11-20
      • 2016-02-26
      • 2016-07-25
      • 2017-03-22
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 2014-03-07
      相关资源
      最近更新 更多