I'm trying to convert a large binary string to a BigInteger
number in C#. After doing some research a line such as the one below should work but for the 2 reasons below it isn't. Am I missing something?
BigInteger bi = new BigInteger("100101000111111110000", 2);
Error CS1503 Argument 1: cannot convert from 'string' to 'System.ReadOnlySpan'
Error CS1503 Argument 2: cannot convert from 'int' to 'bool'
CodePudding user response:
You can try Linq and get the result via Aggregate
:
using.System.Linq;
...
BigInteger bi = "100101000111111110000"
.Aggregate(BigInteger.Zero, (s, a) => (s << 1) a - '0');
CodePudding user response:
The documentation for BigInteger
show the different constructors.
But none of those constructors take a string
and then an int
as parameters. That's what the errors are about. Where did you get the idea the constructor took those arguments?
From what I could see, BigInteger
cannot be called this way.
CodePudding user response:
BigInteger bi = (BigInteger) 1341231.8523;
The above would cast your double to a decimal. However, the BigInteger Struct does not have functionality to convert from binary to integer. It looks like you used the BigInteger Java class functionality.
CodePudding user response:
This BigInteger
belongs to Org.BouncyCastle.Math
namespace in BouncyCastle.Crypto
assembly.
- Install it via NuGet (or in visual studio via NuGet manager)
Install-Package Portable.BouncyCastle -Version 1.9.0
- Usage (convert a binary string to decimal)
var bi = new Org.BouncyCastle.Math.BigInteger("100101000111111110000", 2);
var decimalString = bi.ToString();
var b = double.TryParse(decimalString, out double d);