4 Replies Latest reply: Sep 28, 2011 11:05 AM by 796440

# Hex string conversion to negative long

Hi,

This should result in a negative long value, why is it positive? How can I fix it?

Thanks!!

System.out.println("long value: "+new BigInteger("80000000000000004308000000000000", 16).longValue());

--

long value: 4830110600354856960
• ###### 1. Re: Hex string conversion to negative long
user6102378 wrote:
This should result in a negative long value, why is it positive?
How can I fix it?
Give it a value that will return a negative long.

Winston
• ###### 2. Re: Hex string conversion to negative long
From the doc "Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign."

Are there any other possibilities than using a BigInteger for this?

The value is too big to fit in a parseLong

Thanks!
• ###### 3. Re: Hex string conversion to negative long
user6102378 wrote:
From the doc "Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign."
It also says: "if this BigInteger is too big to fit in a long, only the low-order 64 bits are returned".
Are there any other possibilities than using a BigInteger for this?
For what? All you've done is show us an example where the value is too big for a <tt>long</tt>. Why don't you back up and explain what you're trying to do do, rather than your (plainly flawed) solution.

Winston
• ###### 4. Re: Hex string conversion to negative long
user6102378 wrote:
Are there any other possibilities than using a BigInteger for this?
No. The whole point of BigInteger is for integer values that are arbitrarily large--too large to fit into the fixed-width primitive types. There's nothing else in the language or core API. You might be able to find some thirdparty BigInteger-like class, but you've not indicated why BigIntger doesn't meet your needs, so there's no reason to think you'd find such a class any better, even if you do find it.
The value is too big to fit in a parseLong
No, it's to big to fit in a long. parseLong is an action, not a thing.