Art713
Art713

Reputation: 494

How to Parse string into BigInteger?

I'm writing my magistracy work in cryptography, and today I was trying to get a BigInteger number from the string. But the only way I find to get BigInteger number from the string was

UTF8Encoding Encoding = new UTF8Encoding();
var bytes = Encoding.GetBytes(myString);
var myBigInteger = new BigInteger(bytes);

But this is not that way I need. I need something which will return me exactly BigInteger representation of the string. For example, if I have

var myString = "1234567890";

I want my BigInteger to be

BigInteger bi = 1234567890;

Upvotes: 1

Views: 5691

Answers (3)

Abe Miessler
Abe Miessler

Reputation: 85096

I think TryParse is the way you want to go:

BigInteger number1;
bool succeeded1 = BigInteger.TryParse("-12347534159895123", out number1);

This will not throw an exception if you pass an invalid value. It will just set the number to 0 if the input string will not convert. I believe it is faster than Parse as well.

http://msdn.microsoft.com/en-us/library/dd268301.aspx

Upvotes: 1

Tim B
Tim B

Reputation: 2368

BigInteger bi = BigInteger.Parse("1234567890");

Upvotes: 0

Geeky Guy
Geeky Guy

Reputation: 9399

You could use that type's Parse method.

BigInteger.Parse(string value)

It's static. You pass a string and get the corresponding value.

For example:

BigInteger reallyBigNumber = BigInteger.Parse("12347534159895123");

Upvotes: 6

Related Questions