2011-12-24 6 views
0

이 코드는 일반 텍스트를 가져 와서 512 비트 이진 문자열로 변환합니다. 다음, 문제가 하나이며 그 중 나는 진수 문자열의 8 비트 문자열의 각 32 비트 조각을 설정하고 싶지만, 그 부분은 나에게 java.lang.NumberFormatException16 진수 문자열에서 16 진수 문자열 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))))); 
      } 
     } 

마지막 코드 라인을 제공 I 집행을 해. 어떤 제안?

+2

코드의 마지막 행은이 '}'... –

+1

신은 I가 [I]을 ((32 *의 J를 .substring) 즉 [J] = Long.toHexString (Long.parseLong (청크를 의미 알고있다 (32 * (j + 1)))))); – DanielY

답변

2

* 2의 기수를 지정해야합니다 마지막 문은, 내 생각 :

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

* 아니 Long docs에서

+0

감사합니다. – DanielY

+1

@ user1067083 :) +1 누가 그것을 받아 들인 대답으로 표시한다고 생각합니까? [http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235] – Lion

0

:-) 코드의 마지막 줄, MДΓΓ :

public static long parseLong(String s) throws NumberFormatException :

문자열 인수를 부호있는 십진수 long으로 구문 분석합니다. 캐릭터 라인에있는 문자는 모두 ...

public static long parseLong(String s, int radix) throws NumberFormatException 소수점 자리 수 있어야합니다

는 2 번째의 인수로을 지정 a를 기수 긴 체결로 해, 캐릭터 라인의 인수를 구문 분석합니다. 캐릭터 라인에있는 문자는 모두

당신은 진수 수없는 바이너리를 기대 Long.parseLong()의 첫 번째 버전을 호출하고 ... 지정된 기수의 숫자이어야합니다. 바이너리를 나타 내기 위해 기수가 2 인 인 두 번째 버전을 사용하십시오.

편집 : 이유는 32 자리 10 진수는 Long에 적합하지 않지만 2 진수는 적합합니다.

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