Search code examples
visual-c++c++-clibiginteger

How to convert a string to the BigInteger type in C ++ using the BigInteger Microsoft library


Unfortunately, I have a problem with the BigInteger.Parse(String, NumberStyles) method in the BigInteger library. I tried using it in a C++ program and it does not work. I am not sure, but perhaps it is because Microsoft doesn't provide an opportunity to work with the Parse() method anymore?

How can I convert a string to a BigInteger in C++?

using namespace System::Numerics;

...

static unsigned char hexArr[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };

ostringstream oss;

for (size_t i = 0; i <1024; i++)
    oss << hexArr[rand() % 16];

string str(oss.str());

// Another way to get str, but it doesn't solve the problem
//str += hexTable[rand() % 16];

result = BigInteger::Parse(str, NumberStyles::AllowHexSpecifier);  // My problem here

Solution

  • BigInteger::Parse() doesn't take a std::string as input. It takes a System::String instead. See How to: Convert Standard String to System::String in MSDN'S documentation.

    For example:

    String^ str2 = gcnew String(str.c_str());
    result = BigInteger::Parse(str2, NumberStyles::AllowHexSpecifier);