Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create BigInt with a String encoded to base 16

I am trying to parse a String like this:

f2cff0a43553b2e07b6ae3264bc085a

into a BigInt however when using the String constructor for BigInt I obviously get a Number format exception:

BigInteger bi = new BigInteger("f2cff0a43553b2e07b6ae3264bc085a");

Any ideas how I can do this?

like image 300
SnowyTracks Avatar asked Nov 09 '11 20:11

SnowyTracks


3 Answers

Use the radix parameter:

BigInteger bi = new BigInteger("f2cff0a43553b2e07b6ae3264bc085a", 16);

like image 103
Jonathon Faust Avatar answered Oct 16 '22 16:10

Jonathon Faust


Just use the constructor with the radix (using 16 as radix):

http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html#BigInteger%28java.lang.String,%20int%29

like image 27
JB Nizet Avatar answered Oct 16 '22 15:10

JB Nizet


I think you just need to specify that the string is in hexadecimal. Try

BigInteger bi = new BigInteger("f2cff0a43553b2e07b6ae3264bc085a",16);

http://www.java2s.com/Code/Java/Data-Type/ParsehexadecimalstringtocreateBigInteger.htm http://download.oracle.com/javase/1,5,0/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String, int)

like image 24
jmcdale Avatar answered Oct 16 '22 14:10

jmcdale