Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

Parsing long with MSB set using Long.parseLong()

已查看 0 次
跳至第一个未读帖子

same...@gmail.com

未读,
2005年6月7日 02:49:332005/6/7
收件人
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

0 个新帖子