Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

convert BigInteger to integer  RSS feed

 
Valerie H.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm new to Java and I keep having problems doing conversions.

I am trying to convert a BigInteger to an integer in order to use
modPow(BigInteger exponent, BigInteger). I have enclosed my code below- any help or a recommended place to go would be appreciated.

BigInteger p;
BigInteger g;
BigInteger d;

int l = BigInteger.intValue(d);
BigInteger y = BigInteger.modPow(g l,p);

Thanks in advance.

Valerie
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37044
505
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valerie,
Starting from the API:
public BigInteger modPow(BigInteger exponent, BigInteger m)

First thing to note is that modPow is not a static method. This means that you call it on a specific BigInteger (in this case g.) It takes two BigIntegers (in this case d and p.) You don't need to convert to an int. This is good as you raise to exponents that are larger than can fit in an int. Finally, it returns a BigInteger (which you are storing in y.)

So now you have:
 
Valerie H.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne-

I tried what you suggested and it worked great. Thanks for your help on this- I really appreciate it.

Valerie
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!