4 Replies - 3044 Views - Last Post: 01 November 2011 - 06:00 PM Rate Topic: -----

#1 justinp10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-November 11

Converting from Hex to Decimal using BigInteger

Posted 01 November 2011 - 05:26 PM

Hello,

I am trying to convert a String
 String sn = "71b01d67076bae7f77834fc8de6f7fd5d82c83118debce5b"; 

to Decimal format. The Decimal number appears to be to big to fit into the normal int so I attempted using the BigInteger class in java like so:
public static void main(String[] args){
	BigInteger n;
	try{
		n = new BigInteger(Integer.toString(Integer.parseInt(sn, 16)));
	}catch(NumberFormatException e){
		System.out.println("Number Format Exception in main: ");
		e.printStackTrace();
		throw new RuntimeException(e);
	}
}



And this is the exception I get:

Exception in thread "main" java.lang.RuntimeException: java.lang.NumberFormatException: For input string: "71b01d67076bae7f77834fc8de6f7fd5d82c83118debce5b"
	at ***.******.RSA.main(RSA.java:17)
Caused by: java.lang.NumberFormatException: For input string: "71b01d67076bae7f77834fc8de6f7fd5d82c83118debce5b"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at ***.******.RSA.main(RSA.java:13)



I haven't found any other examples to help convert such a large hexadecimal number to a BigInteger.
I am probably overlooking some minor aspect but could really use another pair of eyes right now.

Thanks,

Is This A Good Question/Topic? 0
  • +

Replies To: Converting from Hex to Decimal using BigInteger

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Converting from Hex to Decimal using BigInteger

Posted 01 November 2011 - 05:31 PM

Integer.parseInt(sn, 16)
has its name suggest parse an integer String an return the int value

As you already mentionned, 71b01d67076bae7f77834fc8de6f7fd5d82c83118debce5b is too big to fit in an integer so you could translate it to an int

However:
BigInteger n = new BigInteger(sn, 16);


should work as the BigInteger class has a constructor that receives the String representation of the number as first parameter and the radix or base as second parameter

And the toString() method of BigInteger returns, by default, the decimal represenation of the number so
System.out.println(n); 


should print its decimal equivalent

*Edited: fixed what wasn't clear. Cannot go much further

This post has been edited by pbl: 01 November 2011 - 06:05 PM

Was This Post Helpful? 1
  • +
  • -

#3 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

Re: Converting from Hex to Decimal using BigInteger

Posted 01 November 2011 - 05:49 PM

The thing is that you're trying to parse the string into an Integer and to do so must both be the string representation of an Integer and it must fit into the memory space reserved for an Integer which is 32 bits. As [member] pbl [member]just said try to convert it using the constructor of BigInteger itself.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Converting from Hex to Decimal using BigInteger

Posted 01 November 2011 - 05:52 PM

Sorry, I tought that my explanations were clear enough
Was This Post Helpful? 0
  • +
  • -

#5 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

Re: Converting from Hex to Decimal using BigInteger

Posted 01 November 2011 - 06:00 PM

Actually it was but [member]#1 justinp10 [member](sorry I don't have flash player installed) as I was saying he didn't say anything so I tried to explain what you said some other way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1