Parsing long with MSB set using Long.parseLong()

S

sameergn

Hi,

I have a small program which goes like this
public class Test
{
public static long x1 = 0xF000000000000001L;

public static void main(String args[])
{
System.out.println("x1 = " + x1);
long x2 = Long.parseLong("F000000000000001", 16);
System.out.println("x2 = " + x2);
}
}

Its output is
x1 = -1152921504606846975
Exception in thread "main" java.lang.NumberFormatException: For input
string: "F000000000000001"
at java.lang.NumberFormatException.forInputString(Unknown
Source)
at java.lang.Long.parseLong(Unknown Source)
at Test.main(Test.java:8)

My intention is to read a bit pattern having MSB set to 1, into a long
variable. The literal long value (x1) allows you to do that in source
file but parseLong() throws exception. Is it because parseLong()
tries to parse "signed" long (as per JavaDoc).
If so, what is a way to read such long value from a file?
I don't want to use DataInput.readLong() method as the value
must be read from a ASCII file.

Thanks,
Sameer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top