Can anyone help me to convert String to BigInteger. please

Recommended Answers

All 10 Replies

In what way is the BigInteger constructor which accepts a string not working out for you?

commented: the constructor that receives a char array was definitly the right answer all along ;) +6

Convert it to long first using Long.parseLong() and then convert the long to BigInteger using BigInteger.valueOf(long val) .

Cheers! :)

thanks dear it worked, but isn't there any direct way for such a conversion??

Convert it to long first using Long.parseLong() and then convert the long to BigInteger using BigInteger.valueOf(long val)

This won't work if the string representation is outside the "long" data type range, which is one of the main motivation for using BigInteger...

thanks dear it worked, but isn't there any direct way for such a conversion??

There is no direct way to do these conversions given that like C++, Java doesn't support operator overloading.

thanks dear it worked, but isn't there any direct way for such a conversion??

It worked. -.-

So? It still doesn't change the fact that it will fail for certain inputs as mentioned above and the worst thing is that regular tests which don't exercise the limits won't be able to uncover this problem.

Please don't provide sub-standard advice to beginners who wouldn't know the difference between "working" and "correct" code. It's a pity that my post, which tried to push adil in the right direction was downvoted. If someone wants to challenge my post above, I would be more than happy to provide reasons/discuss.

He wanted to find out the value of something raised to the power of something, and those something's were well within long range. And by the way, please be a little more polite. I am not challenging your post.

Please don't conflate strong disagreement and resetment towards advice given with impoliteness.

Plus, my first post was meant to push OP to do a bit more research about BigIntegers and you pretty much handed the answer. Questions posted (especially by studnents) are much more about the learning involved in reaching the answer rather than "getting" the answer.

Well, I'm sorry for that, but you sounded like a little rude to me. So I hit back at you. Apologies for any mistake made by me.

commented: No worries +17

i have researched it a lot but was not getting the right thing, so i need to take the help of the forum, and if anyone wants me to go on researching, then its useless for me to be in the forum. Sometimes the answers are to be handed away.

commented: i disagree, being able to find answers on your own at your level is the best skill you can work on, if we hand you answers now, you will advanced to more advanced programming classes and find yourself on this forum asking for answers 24/7! -1
commented: yeah +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.