Parsing a long value

K

Ken Hygh

I'm trying to get a long value from a hex string, and so far I can't
figure out my problem. When I try

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

I get a NumberFormatException. If I change that leading 'd' to a
number <= 7, then the code works fine.

What am I doing wrong?

Thanks,
Ken
 
M

Michael Borgwardt

Ken said:
long l = Long.parseLong("d2b19756c550c101",16);

I get a NumberFormatException. If I change that leading 'd' to a
number <= 7, then the code works fine.

What am I doing wrong?

The number you're trying to parse is too big for a long to hold.
Remember that all Java integer primitive types (except char, which
should not be used like an integer type) are signed.

You'll have to use a workaround like this (untested):

long l = Long.parseLong("c550c101",16") &
(Long.parseLong("d2b19756",16") << 32)
 
P

P.Hill

Ken said:
I'm trying to get a long value from a hex string, and so far I can't
figure out my problem. When I try

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

"The largest positive hexadecimal and octal literals of type long are
0x7fffffffffffffffL and 0777777777777777777777L, respectively, which equal
9223372036854775807L (263-1). The literals 0x8000000000000000L and
01000000000000000000000L are the most negative long hexadecimal and octal
literals, respectively. Each has the decimal value -9223372036854775808L (-263).
The hexadecimal and octal literals 0xffffffffffffffffL and
01777777777777777777777L, respectively, represent the decimal value -1L."

-- http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282

It looks to long to me.

HTH,
-Paul
 
C

Chris Smith

Michael said:
The number you're trying to parse is too big for a long to hold.
Remember that all Java integer primitive types (except char, which
should not be used like an integer type) are signed.

You'll have to use a workaround like this (untested):

long l = Long.parseLong("c550c101",16") &
(Long.parseLong("d2b19756",16") << 32)

Just to head this one off, that '&' should be '|'.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top