2011-12-24 233 views
0

我有这里的代码,它得到一个纯文本,并将其转换为一个512位的二进制字符串。 然后,我想每个32位块的串的转向十六进制字符串的8位,但该部分给我一个java.lang.NumberFormatException二进制字符串到十六进制字符串java

// ----- 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))))); 
      } 
     } 

最后代码行是有问题的一个和其中我得到执行。有什么建议么?

+2

的最后一行代码是一个''} ... –

+1

天知道我的意思的话[J] = Long.toHexString(的Long.parseLong(块[I] .substring((32 * j)中, (32 *(j + 1))))); – DanielY

回答

2

最后声明*应指定的2基数,我认为:

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

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

+0

非常感谢,它的工作! – DanielY

+1

@ user1067083 :) +1谁会考虑将其标记为已接受的答案,如果有效? [http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235] – Lion

0

Long docs

public static long parseLong(String s) throws NumberFormatException

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

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

分析由第二个参数指定字符串参数作为有符号长在基数。字符串中的字符必须为指定基数的数字...

你打电话的Long.parseLong()的第一个版本,这需要一个十进制数,不是非此即彼。使用的第二个版本为2来表示二进制。

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

0
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)); 
    } 
} 
相关问题