0

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'

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
UniqueHold
  • 33
  • 5
  • 1
    Neither the `BigInteger` constructor nor its `Parse` method have an overload that accepts a `string` and numeric base. Are you sure you're looking at .NET documentation, and not `BigInteger` from Java or some other framework? – Ben Voigt Sep 30 '22 at 21:41
  • Your code is Java, not C#. – President James K. Polk Sep 30 '22 at 21:42
  • I don't see a constructor accepting string and integer for [`BigInteger`](https://learn.microsoft.com/en-us/dotnet/api/system.numerics.biginteger?view=net-6.0#constructors). Where have you found this code? Is this from [Java one](https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String,%20int))? – Guru Stron Sep 30 '22 at 21:42
  • Would it work in Java then? – UniqueHold Sep 30 '22 at 21:57
  • Related: [Faster way to convert large binary string to BigInteger](https://stackoverflow.com/questions/56333862/faster-way-to-convert-large-binary-string-to-biginteger) – Theodor Zoulias Sep 30 '22 at 22:03
  • @UniqueHold Yes, it will work in Java. – reakt Sep 30 '22 at 22:18
  • @TheodorZoulias but the answer shows exactly what OP needed. – Guru Stron Sep 30 '22 at 23:30
  • 1
    @TheodorZoulias we can use [this one](https://stackoverflow.com/a/33612030/2501279) if you prefer)) Though it links previous one) – Guru Stron Sep 30 '22 at 23:38

4 Answers4

2

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');
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0
 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.

reakt
  • 500
  • 8
  • 25
-2

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);
Muhammad Sulaiman
  • 2,399
  • 4
  • 14
  • 28
  • 1
    The BigInteger struct is in the System.Numerics namespace. – reakt Sep 30 '22 at 22:14
  • 1
    Really, And what if there is another BigInteger in another assembly that takes first parameter as string and the second one as int? If you read the question well, it says "After doing some research a line such as the one below should work but".. but could not run it.. so I've make it clear why he could not run it.. – Muhammad Sulaiman Sep 30 '22 at 22:16
  • 1
    Considering BigInteger is in the native System.Numerics namespace, should we be quick to jump to the conclusion that he was using the third party BouncyCastle.Crypto assembly? However, your suggestion does work. It looks like this piece of functionality in BouncyCastle was ported from Java. – reakt Sep 30 '22 at 22:33
  • 1
    He uses nothing, he just found a piece of code out there and wonders why it's not working.. As the only explanation for the existence of such `BigInteger(string, int);` is that it belongs to the popular bountycastel assembly: "jumping fast" to a correct explanation is smart, isn't it? – Muhammad Sulaiman Sep 30 '22 at 22:51
  • @MuhammadSulaiman I don't agree with your last comment - OP for example could have been confused by some java code, for example. Just tweak the answer wording a bit (to something like "you can use BigInt from....") and I would argue it will have much more appeal. – Guru Stron Sep 30 '22 at 23:36
  • @MuhammadSulaiman Good point. Although I suspect it is more likely he found an answer for Java while Googling or searching for answers here. – reakt Sep 30 '22 at 23:37