2015-12-14 5 views
5

をintに進文字列を変換するとき、私は小数に進文字列に変換したいのですが、私は次のコードでエラーました:java.lang.NumberFormatException

String hexValue = "23e90b831b74";  
int i = Integer.parseInt(hexValue, 16); 

エラー:

Exception in thread "main" java.lang.NumberFormatException: For input string: "23e90b831b74" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:495) 

答えて

13

23e90b831b74が大きすぎてintに収まりません。

数字を数えることで簡単に確認できます。 16進数の各2桁には1バイトが必要なので、12桁には6バイトが必要ですが、intには4バイトしかありません。

Long.parseLongを使用してください。

String hexValue = "23e90b831b74";  
long l = Long.parseLong(hexValue, 16); 
3

これは、「文字列を整数として解析できない」場合に発生します。他の理由の中でも、値がInteger.MAX_VALUEまたはInteger.MIN_VALUEを超える場合、これが当てはまります。

intとして解析可能な最大数は2147483647(231-1)であり、最大の長さは9223372036854775807(263-1)で、長さの約2倍です。任意の長い数値を解析するには、BigIntegerを使用します。

import java.math.BigInteger; 
BigInteger number = new BigInteger(hexValue); 
関連する問題