Mojackoo
Mojackoo

Reputation: 93

Convert a large number to hex in Java

As we know a Max of long is 9223372036854775807

in my case I want to convert this number dec = 11265437495266153437 to hex using this method Integer.toHexString(dec)

any idea to how get this reasult res = 9C56DFB710B493DD !

Upvotes: 0

Views: 785

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79095

BigInteger::toString( radix )

Call BigInteger::toString and pass 16 to get hexadecimal text.

Do it as follows:

import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        String value = 
                new BigInteger("11265437495266153437", 10)
                .toString(16)
                .toUpperCase()
        ;
        System.out.println(value);
    }
}

Output:

9C56DFB710B493DD

Note that the default radix is 10 so you can skip it and use new BigInteger("11265437495266153437") instead which is without any radix parameter.

Upvotes: 3

Related Questions