ZZ Coder ZZ Coder - 2 months ago 5x
Java Question

How to Parse Negative Long in Hex in Java

We have a J2ME application that needs to read hex numbers. The application is already too big for some phones so We try not to include any other codec or write our own function to do this.

All the numbers are 64-bit signed integers in hex, when we use Long.ParseLong(hex, 16), it handles positive numbers correctly but it throws exception on negative numbers,

long l = Long.parseLong("FFFFFFFFFFFFFFFF", 16);

How can we get -1 from that hex string using classes provided in Java itself?

Some people may suggest we should write our hex as -1 as Java expected. Sorry, the format is fixed by the protocol and we can't change it.


Your problem is that parseLong() does not handle two's complement - it expects the sign to be present in the form of a '-'.

If you're developing for the CDC profile, you can simple use

long l = new BigInteger("FFFFFFFFFFFFFFFF", 16).longValue()

But the CLDC profile doesn't have that class. There, the easiest way to do what you need is probably to split up the long, parse it in two halves and recombine them. This works:

long msb = Long.parseLong("FFFFFFFF", 16);
long lsb = Long.parseLong("FFFFFFFF", 16);
long result = msb<<32 | lsb;


As of Java 8, you can use parseUnsignedLong():

long l = Long.parseUnsignedLong("FFFFFFFFFFFFFFFF", 16);