2014-11-16 5 views
0

바이트 배열을 정수 형식의 16 진수로 변환하는 다음 함수가 있습니다.16 진수 배열을 16 진수로 (int 형식으로)

private static int byteArray2Int(final byte[] hash) { 
     Formatter formatter = new Formatter(); 
     for (byte b : hash) { 
      formatter.format("%02x", b); 
     } 

     String str = formatter.toString(); 
     int hex = Integer.parseInt(str, 16); //number format exception 

     return hex; 
    } 

-

그리고 오류가 아래 얻고있다. 포맷터 값이 이미 16 진수임을 알고 있지만 정수 형식으로 저장하고 싶습니다.

어떻게 진행하나요?

Exception in thread "main" java.lang.NumberFormatException: For input string: "202e4724bb138c1c60470adb623ac932" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) 
+0

'202e4724bb138c1c60470adb623ac932'는'int '가 아니기 때문에이 예외가 발생합니다. – Maroun

답변

1

문자열이 int 범위 내에서 너무 길어서 int에 저장하려고 시도하는 대신 BigInteger를 다음과 같이 사용하십시오.

String hex = "202e4724bb138c1c60470adb623ac932"; 
BigInteger bi = new BigInteger(hex, 16); 
System.out.println(bi); 
0

는 "202e4724bb138c1c60470adb623ac932"는 int 또는 long을에 맞게 너무 큽니다. 16 바이트가 필요할 것입니다 (올바르게 계산 한 경우).