0

I tried to convert Hex to BigInteger, here is source code:

public static void main (String[] args)
{
    BigInteger a = new BigInteger("c6e87a3b3ef791287ac85a0c7836dd8305dd9a4c0024123db9145bb5067a8abf142e9001b0788039fd4d676b59db2110da23532282c7648d94fdbf29b731b0d21f9ca51acd44063f271326915283af97f0822519bbe2a6b80618e45e6194b2445d5afe70cf2c10569034966f3bc3b9a30d3ac4f06dbbca89fce7ef64ee14de3dL", 16);
    BigInteger b = new BigInteger("9e5d1bd4f53eebc8a695c61ba4436e38af273fd6733115611fded8dd407b5f0bc04301829dc6ed921af866c3c7977839fc75831152307f8e50e3c0f9107b6ae82ddab584807ea5ba7f32f9bfcab6218c6c6367817dfdd3b2ccc5c21cc9550b9248cac34dfb0d22151c196ca843f15614b3f6b044f9c5e727dc0b44f441c2ed7fL", 16);
    System.out.println(a);
    System.out.println(b);
    // System.out.println(modInv(a, b));
}

I tried to run this source code, but I got Runtime error like this:

Exception in thread "main" java.lang.NumberFormatException: For input string: "....14de3dL" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.math.BigInteger.(Unknown Source) at sss.main.main(main.java:15)

Is there something problem in source code? I can't find it.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jinseo Kim
  • 35
  • 4

2 Answers2

1

First of All, L is not included in Hex Numbers that you wrote in the end of both strings. you can visit this link for help on Hex Numbers

https://en.wikipedia.org/wiki/Hexadecimal

After visiting this , please view this post as well

Java convert a HEX String to a BigInt

1

As pointed out in comment L is not part of Hexadecimal presentation.L is used to represent the string as Long, so it is an invalid character in the hexadecimal presentation. Code below should be helpful

 BigInteger a = new BigInteger("c6e87a3b3ef791287ac85a0c7836dd8305dd9a4c0024123db9145bb5067a8abf142e9001b0788039fd4d676b59db2110da23532282c7648d94fdbf29b731b0d21f9ca51acd44063f271326915283af97f0822519bbe2a6b80618e45e6194b2445d5afe70cf2c10569034966f3bc3b9a30d3ac4f06dbbca89fce7ef64ee14de3d", 16);
 System.out.println(a.toString());
Denis
  • 1,219
  • 1
  • 10
  • 15
  • This method does not work on some string like "0x00" ( hex zero). Couldn't figure it out why. – Mahdi Jun 09 '23 at 04:56