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'

8
  • 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? Commented Sep 30, 2022 at 21:41
  • Your code is Java, not C#. Commented Sep 30, 2022 at 21:42
  • I don't see a constructor accepting string and integer for BigInteger. Where have you found this code? Is this from Java one? Commented Sep 30, 2022 at 21:42
  • Would it work in Java then? Commented Sep 30, 2022 at 21:57
  • 1
    @TheodorZoulias we can use this one if you prefer)) Though it links previous one) Commented Sep 30, 2022 at 23:38

4 Answers 4

3

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');
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! This one did the trick. I didn't know it could be used this way. I appreciate your response.
That works... so long as the source string consists solely of 0 and 1.
@NicholasCarey OP specifies binary string. I assume it should consist only of 1s and 0es, no?
@GuruStron — Do you trust your input? I don't
@NicholasCarey it depends.
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.

Comments

-1
 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.

1 Comment

I don't see any double in the question, so how does this answer the q?
-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);

6 Comments

The BigInteger struct is in the System.Numerics namespace.
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..
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.
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?
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.