Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java convert a HEX String to a BigInt

Tags:

java

Hi am trying to convert a hex string such as String hexStr = "1b0ee1e3"; to a bigInt, ideally i'd like to convert hexStr to a bigint in its decimal form,

I can convert a string to a bigInt w/o issues but when the string contains hex values i run into problems

like image 925
user524156 Avatar asked Nov 30 '10 17:11

user524156


People also ask

Can we convert BigInteger to string in Java?

BigInteger. toString() method returns the decimal String representation of this BigInteger. This method is useful to convert BigInteger to String. One can apply all string operation on BigInteger after applying toString() on BigInteger.


1 Answers

Have you tried:

BigInteger bigInt = new BigInteger(hexString, 16); 

For example:

import java.math.*;  public class Test {     public static void main(String[] args) {         String hexStr = "1b0ee1e3";         BigInteger bigInt = new BigInteger(hexStr, 16);         System.out.println(bigInt); // Prints 453960163     } } 
like image 183
Jon Skeet Avatar answered Sep 19 '22 17:09

Jon Skeet