8

I'm using following code to create a BigInteger from hexadecimal string and print in to output.

package javaapplication2;

import java.math.BigInteger;
import javax.xml.bind.DatatypeConverter;

public class JavaApplication2 {
    public static void main(String[] args) {
        // Number in hexadecimal form
        String HexString = "e04fd020ea3a6910a2d808002b30309d";
        // Convertation from string to byte array
        byte[] ByteArray = toByteArray(HexString);
        // Creation of BigInteger from byte array
        BigInteger BigNumber = new BigInteger(ByteArray);
        // Print result
        System.out.print(BigNumber + "\n");
    }
    public static String toHexString(byte[] array) {
        return DatatypeConverter.printHexBinary(array);
    }

    public static byte[] toByteArray(String s) {
        return DatatypeConverter.parseHexBinary(s);
    }
}

After execution of this code I'm get a following result:

-42120883064304190395265794005525319523

But I'm expected to see this result:

298161483856634273068108813426242891933

What I'm doing wrong?

2 Answers 2

25

You're passing in a byte array where the first byte has a top bit that is set - making it negative. From the constructor documentation:

Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. The input array is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.

A two's-complement binary representation with a leading set bit is negative.

To get the result you want, you can do any of:

  • Prefix the hex string with "00" so that you'll always get a top byte of 0
  • Pass the hex string straight into the BigInteger(String, int) constructor, where the sign is inferred from the presence or absence of "-" at the start of the string. (Obviously you'd pass in 16 as the base.)
  • Use the BigInteger(int, byte[]) constructor, passing 1 as the signum value

If your real context is that you've already got the byte array, and you were only parsing it from a hex string for test purposes, I'd use the third option. If you've genuinely got a hex string as input, I'd use the second option.

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

1 Comment

Thank you! Following your answer I fixed this problem.
5

try

BigInteger bigInt = new BigInteger(HexString, 16);

4 Comments

Very interesting. I didn't know the existence of this constructor. Well done!
Thank you! I'm like your answer and will use it in future.
Why did someone downvote the correct answer? You really shouldn't do that.
I don't know why it was downgradred, but text of the question was stating; " create a BigInteger from hexadecimal string" ... it didn't not say it had to pass thought byte[]... if it was for the missing explanation, I really don't know how to explain that the method exist in the class :(

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.