theycallhimtom
theycallhimtom

Reputation: 345

C++ string to boolean array

I have a ~100 digit string which represents a number in base 10 that I want to convert to either a string representing the number in base 2, or a bool array which represents the number's digits in binary. I can do it easily in Java using BigInteger, but I'm not sure if there is an equivalent in C++.

Function would be something like:

string toBinaryString(string numInDecimal);

Thanks for the help.

Upvotes: 2

Views: 1829

Answers (3)

f3lix
f3lix

Reputation: 29877

Use the GNU Multiple Precision Arithmetic Library (GMP) available at http://gmplib.org. Then mpz set str is probably what you need to create a "BigInteger" from your string. And mpz get str to create a string again. Both functions take the base as parameter.

Upvotes: 2

AlexDrenea
AlexDrenea

Reputation: 8039

Unfortunately there is no standard C++ class/function to do that. Anyway, to give you at least some little help here are some more or less useful starting points to develop your own bigInt C++ class :

StackOverflow : How to implement big int in C++

SourceForge : C++ BigInt class

Matt McCutchen : BigInt Class

Upvotes: 1

Calyth
Calyth

Reputation: 1673

Uh... that's a hell a lot of digits in binary, and you're going to have some fun time handling it.

Either that, or you can use GMP to help you along...

Upvotes: 0

Related Questions