0

I'm trying to port a code from .NET which uses UInt64 everywhere, I've mostly succeeded using BigInteger, but unfortunately I've become stuck at something really simple as loading a BigInteger with a value from a table of hex values.

At some point I want to load a BigInteger with the positive 0x990951BA, only way I saw to do this was with BigInteger b = BigInteger.valueOf(0x990951BA), this of course does not work because Java treats the argument as a long, making it negative.

How can I achieve this?

Thank you.

SoManyGoblins
  • 5,605
  • 8
  • 45
  • 65

2 Answers2

2

long is 64-bit in Java, so you should be able to use it as a direct substitute for UInt64. To specify a long literal, append an l to the constant to have it recognized as long rather than int.

BigInteger b = BigInteger.valueOf(0x990951BAl);

A fallback for values which are too big even for long is to parse it as text.

BigInteger b = new BigInteger("990951BA", 16);
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • Been working with C# far too long! :) thank you, I hadn't realised Java was treating my hex value as an int instead of a long! – SoManyGoblins Apr 16 '12 at 12:37
0

Since you're porting code that uses unsigned longs, you may also want to look into UnsignedLong from Google Guava. (And also the UnsignedLongs helper class in the same package).

daveb
  • 74,111
  • 6
  • 45
  • 51