0

I have a hex string that represents a 64-bit unsigned int.

I want to convert this to a decimal representation in java.

I wanted to use Long.parseLong("HexString", 16); but in java the leftmost bit is the sign bit and any number greater than 2^31-1 would not fit.

SO how do i convert and store this data?

Thanks, Sunny

1 Answer 1

9

You can create a BigInteger using the constructor that takes a String and an int radix.

BigInteger bigInt = new BigInteger(hexString, 16);

If you have Java 8, you can use Long.parseUnsignedLong, which will allow you to parse all 64 bits, even if the bit that would normally put it over Long.MAX_VALUE is set. It still returns a long, which in Java is still signed, but it will be parsed as if it were an unsigned long.

long stillSigned = Long.parseUnsignedLong(hexString, 16);

If the value that gets parsed is negative, then the true unsigned value is the signed value + 264.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.