It works like a charm but it is not very safe

. BigInteger.longValue() returns only the lowest 64 bits while BigInteger is meant to represent arbitrary-precision integers. This means you can get a wrong value, not even of the same magnitude and it might be even with the wrong sign (+/-).
A simpler solution is to use BigInteger.toString(int radix).
import java.math.BigInteger;
public class BIHex {
public static void main(String[] args) {
java.math.BigInteger bi = new BigInteger(
"12345678901234567890123456789012345678901234567890" +
"12345678901234567890123456789012345678901234567890");
System.out.println("max long = " + Long.toHexString(Long.MAX_VALUE));
System.out.println("bi as long = " + Long.toHexString(bi.longValue()));
System.out.println("bi as hex = " + bi.toString(16));
}
}
When you run this class you see the difference:
max long = 7fffffffffffffff
bi as long = accff196ce3f0ad2
bi as hex = 241fc1742fe8d29593a6afe52b31741cfe5a7f8e67e477381be47851641ef7bf14baccff196ce3f0ad2