Reputation: 494
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
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
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