9

I try to run this code but it occur error.

System.out.println(Long.parseLong("9000000000000000", 16));

As we know that the minimum number of long is -9,223,372,036,854,775,808 and 0x9000000000000000 is -8,070,450,532,247,928,832 why it will occur error?

Rajesh
  • 724
  • 8
  • 30
Moly Holy
  • 133
  • 1
  • 9

3 Answers3

10

9000000000000000 base 16 is a positive number since there is no sign. Since a long is signed, the greatest number that it can hold is 0x7FFF_FFFF_FFFF_FFFF. So yours is too great.

If you want -8,070,450,532,247,928,832, use parseUnsignedLong():

        System.out.println(Long.parseUnsignedLong("9000000000000000", 16));

Output:

-8070450532247928832

Now values up to 0xFFFF_FFFF_FFFF_FFFF are accepted.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
2

Referring to Long#parseLong(String,int)

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002d') or plus sign '+' ('\u002B') provided that the string is longer than length 1.
  • The value represented by the string is not a value of type long.

Examples:
  parseLong("0", 10) returns 0L
  parseLong("473", 10) returns 473L
  parseLong("+42", 10) returns 42L
  parseLong("-0", 10) returns 0L
  parseLong("-FF", 16) returns -255L
  parseLong("1100110", 2) returns 102L
  parseLong("99", 8) throws a NumberFormatException
  parseLong("Hazelnut", 10) throws a NumberFormatException
  parseLong("Hazelnut", 36) returns 1356099454469L

The decimal value parsed using radix 16 is 10376293541461622784 which is greater than Long.MAX_VALUE(9223372036854775807), violate following condition:

The value represented by the string is not a value of type long

hence throwing NumberFormatException.

import java.math.BigInteger;

public class ParseLong {
    public static void main(String[] args) {
        System.out.println("Max Long value :" + Long.MAX_VALUE);
        System.out.println("Min Long value :" + Long.MIN_VALUE);
        System.out.println("Value without overflow " + new BigInteger("9000000000000000", 16));
        System.out.println("Value by parseUnsigned " + Long.parseUnsignedLong("9000000000000000", 16));
        System.out.println("Value by literal " + 0x9000000000000000L);
    }
}
samabcde
  • 6,988
  • 2
  • 25
  • 41
1

Long.parseLong() does not "overflow" around to a negative number like arithmetic does - it's not a parsing a bit representation but rather the digits of a whole number.

The largest long in base 16 is 7FFFFFFFFFFFFFFF; your value is bigger than that.

For comparison in decimal:

Base 16            Decimal
7FFFFFFFFFFFFFFF    9,223,372,036,854,775,807
9000000000000000   10,376,293,541,461,622,784
Bohemian
  • 412,405
  • 93
  • 575
  • 722