Java negative int to hex and back fails

后端 未结 6 1744
北荒
北荒 2020-12-06 06:54
public class Main3 {
    public static void main(String[] args) {
        Integer min = Integer.MIN_VALUE;
        String minHex = Integer.toHexString(Integer.MIN_VA         


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 07:28

    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
    

提交回复
热议问题