Converting large binary strings to integers (java)

Associate
Joined
19 Jun 2006
Posts
162
Location
Swansea, Wales
I need to convert some large strings of binary numbers to integers.

I was using Integer.parseInt(myString, 2) which was working fine but now I have strings up to 32 bits long and I get a numberFormatException thrown. I imagine it is because the size of the number exceeds the memory allocated for an integer. I can't use BigInteger because .parseInt will only return an int.

Any ideas how to solve this problem?
 
I think from something similar I did earlier this year you have to use Long as opposed to Integer, so Long.parseLong(myString,2);
 
If you want your app to be truly scaleable (and to avoid having a similar problem if your numbers outgrow the long datatype), perhaps you should consider using a BigInteger. It has a constructor that takes a string and a radix.
 
Back
Top Bottom