Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String UUID to BigInteger in Java

I am trying to convert UUID which is coming as string to Big Integer but it's failing every time with Number Format exception as it need String Decimal as parameter. Is there any way we can achieve this.

String x = "6CFAFD0DA976088FE05400144FFB4B37";

I tried with radix also but output is different.

BigInteger big = new BigInteger(x, 0);

System.out.println(big);

Any help is appreciated, TIA.

like image 312
Rohit Kotak Avatar asked Dec 04 '22 19:12

Rohit Kotak


2 Answers

You are supposed to be using radix 16 as your string has alphanumeric values from 0-9 and A-F, set value 16 in radix as you have hexadecimal string.

String x = "6CFAFD0DA976088FE05400144FFB4B37";
BigInteger big = new BigInteger(x, 16);
System.out.println(big);

OUTPUT

144859830291446118078300087367740640055
like image 80
akash Avatar answered Dec 26 '22 18:12

akash


You need to set radix value to 16.

For hexadecimal String you need to define the radix value as 16

like image 40
Paras Avatar answered Dec 26 '22 20:12

Paras