Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多个十六进制字符的NumberFormatException_Java_Android - Fatal编程技术网

Java 多个十六进制字符的NumberFormatException

Java 多个十六进制字符的NumberFormatException,java,android,Java,Android,当我在转换为整数值的字符串中插入许多十六进制字符时,我收到一个异常 代码: 我需要这个字符串的值不大于size int(32位)。有什么建议吗 try { // try your calculation here } catch (NumberFormatException e) { // assign the variable the max/min value instead // or whatever behavior you want } 此代码将为超出范围的

当我在转换为整数值的字符串中插入许多十六进制字符时,我收到一个异常

代码:

我需要这个字符串的值不大于size int(32位)。有什么建议吗

try {
    // try your calculation here
} catch (NumberFormatException e) {
    // assign the variable the max/min value instead
    // or whatever behavior you want
}

此代码将为超出范围的数字提供默认行为。

在我看来,您对十六进制字符串到整数的转换使用了错误的方法,如果我的此理解错误,请告诉我

我引用了一个重复的帖子

    String hex = "ff"

// small values
    int value = Integer.parseInt(hex, 16);  

// big values
    BigInteger value = new BigInteger(hex, 16);


格雷格·休吉尔的评论是正确的。看起来您正在尝试自动解释位。可能最简单的方法就是

 Long.valueOf(displayValue + "F", currentBase).intValue();

获取位,然后将其截断为int。当然,现在您有责任确保
Long.valueOf
的参数适合32位。

ffffff太大,最大int值为0x7FFFFFFF解决方法是

int i = (int)Long.parseLong(hexStr, 16);

把你的电话号码调小一点?你在问什么?这个数字是用户输入的,我试图在一个支持双字(32位)的计算器中使用这个值。正如你已经知道的,Java
int
值的范围从-2147483648到2147483647。十六进制值0xFFFFFF是4294967295,超出此范围。我如何获取0xFFFFFF并使用它表示其负值(-1)?谢谢,我知道0x7FFFFFF是最大整数值。我如何取0xFFFFFF并得到-1的int值?(int)Long.parseLong(“FFFFFFFFF”,16)给出-1,谢谢!我让一切都运转起来了,现在对这些转换是如何工作的有了更好的理解!
 Long.valueOf(displayValue + "F", currentBase).intValue();
int i = (int)Long.parseLong(hexStr, 16);