Java 负整数到十六进制并返回失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/845230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:01:26  来源:igfitidea点击:

Java negative int to hex and back fails

javaparsingdecimalhexsigned

提问by Maxim Veksler

public class Main3 {
    public static void main(String[] args) {
        Integer min = Integer.MIN_VALUE;
        String minHex = Integer.toHexString(Integer.MIN_VALUE);

        System.out.println(min + " " + minHex);
        System.out.println(Integer.parseInt(minHex, 16));
    }
}

Gives

-2147483648 80000000
Exception in thread "main" java.lang.NumberFormatException: For input string: "80000000"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:459)
    at Main3.main(Main3.java:7)

Whats up?

这是怎么回事?

回答by dhn

It's documented that Integer.toHexStringreturns a string representation of the integer as an unsigned value - while Integer.parseInttakes a signed int. If you use Integer.toString(value, 16)instead you'll get what you want.

它记录了Integer.toHexString将整数的字符串表示返回为无符号值 - 而Integer.parseInt采用有符号整数。如果你Integer.toString(value, 16)改用你会得到你想要的。

回答by Alan Moore

This is something that's always annoyed me. If you initialize an int with a hex literal, you can use the full range of positive values up to 0xFFFFFF; anything larger than 0x7FFFFFwill really be a negative value. This is very handy for bit masking and other operations where you only care about the locationsof the bits, not their meanings.

这是一直让我恼火的事情。如果使用十六进制文字初始化 int,则可以使用最大范围的正值0xFFFFFF;任何大于0x7FFFFF实际上是负值。这对于位掩码和其他您只关心位的位置而不是它们的含义的操作非常方便。

But if you use Integer.parseInt() to convert a string to an integer, anything larger than "0x7FFFFFFF"is treated as an error. There's probably a good reason why they did it that way, but it's still frustrating.

但是,如果您使用 Integer.parseInt() 将字符串转换为整数,则任何大于此值的内容"0x7FFFFFFF"都会被视为错误。他们这样做可能有很好的理由,但仍然令人沮丧。

The simplest workaround is to use Long.parseLong() instead, then cast the result to int.

最简单的解决方法是改用 Long.parseLong(),然后将结果转换为 int。

int n = (int)Long.parseLong(s, 16);

Of course, you should only do that if you're sure the number is going to be in the range Integer.MIN_VALUE..Integer.MAX_VALUE.

当然,只有在确定数字将在范围内时才应该这样做Integer.MIN_VALUE..Integer.MAX_VALUE

回答by Sylvain Leroux

According the the documentation, toHexStringreturns "a string representation of the integer argument as an unsignedinteger in base 16. "

根据文档,toHexString返回“整数参数的字符串表示,作为基数为 16的无符号整数。”

So the correct reverse operation is probably Integer.parseUnsignedIntthat was introduced as part of Java 8:

所以正确的反向操作可能Integer.parseUnsignedInt是作为 Java 8 的一部分引入的:

public class Main3 {
    public static void main(String[] args) {
        Integer min = Integer.MIN_VALUE;
        String minHex = Integer.toHexString(Integer.MIN_VALUE);

        System.out.println(min + " " + minHex);
        System.out.println(Integer.parseUnsignedInt(minHex, 16));
    }

回答by alphazero

Try this:

试试这个:

public class Main3 {
    public static void main(String[] args) {
        Integer min = Integer.MIN_VALUE;
        String minHex = Integer.toHexString(Integer.MIN_VALUE);

        System.out.println(min + " " + minHex);
        System.out.println(Integer.parseInt( "-" + minHex, 16));
    }

}

}

to get this:

得到这个:

-2147483648 80000000
-2147483648

回答by Michael Haren

You need to include a negativesign.

您需要包含一个负号。

I don't have access to test this right now but I'd bet if you tried this value instead:

我现在无权测试这个,但我敢打赌,如果你尝试这个值:

Integer min = Integer.MIN_VALUE + 1;

It wouldn't bomb, but would give you a positive number (not negative) when you ran ParseInt(min,16).

它不会爆炸,但会在你运行时给你一个正数(不是负数)ParseInt(min,16)

A stringof bits doesn't really have enough info to determine sign in this context so you need to provide it. (consider the case where you use min = "F". Is that +/-F? If you converted it to bits and saw 1111, andyou knew it was a byte, you might conclude that it's negative, but that's a lot of ifs.

一个位并没有真正有足够的信息,以确定在这方面迹象,所以你需要提供它。(考虑一下您使用的情况min = "F"。那是 +/-F 吗?如果您将其转换为位并看到 1111,并且您知道它是一个字节,您可能会得出结论它是负数,但这是很多 if。

回答by Fabrice LARRIBE

This seem to work for me :

这似乎对我有用:

public class Main3 {
public static void main(String[] args) {
    Integer min = Integer.MIN_VALUE;
    String minHex = Integer.toHexString(Integer.MIN_VALUE);

    System.out.println(min + " " + minHex);
    System.out.println((int)Long.parseLong(minHex, 16));
}
}

The integer is parsed as a "signed long" that handle such big positive number and then the sign is found back by casting it to "int".

整数被解析为处理如此大的正数的“signed long”,然后通过将其转换为“int”来找到符号。