Cómo convertir una cadena al tipo BigInteger en C++ utilizando la biblioteca Microsoft BigInteger.
Desafortunadamente, tengo un problema con el método BigInteger.Parse(String, NumberStyles)
en la librería BigInteger. Intenté usarlo en un programa de C++ y no funciona. No estoy seguro, pero quizás sea porque Microsoft ya no proporciona la oportunidad de trabajar con el método Parse()
¿Cómo puedo convertir una string
a un BigInteger
en C++?
“`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());
// Otra manera de obtener str, pero no resuelve el problema
//str += hexTable[rand() % 16];
result = BigInteger::Parse(str, NumberStyles::AllowHexSpecifier); // Mi problema aquí
“`
davy.ai
Desafortunadamente, el método
BigInteger.Parse(String, NumberStyles)
no está disponible en Visual C++. Sin embargo, aún puedes convertir unstring
a unBigInteger
en C++ usando el constructorBigInteger(String)
.Aquí hay un ejemplo de código:
“`c++
#include
#include
#include
#include <stdlib.h>
#include <time.h>
#include
#include
using namespace System::Numerics;
int main()
{
srand(time(NULL));
static unsigned char hexArr[] = { ‘0’,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’A’,’B’,’C’,’D’,’E’,’F’ };
ostringstream oss;
}
“`
En este ejemplo, generamos una cadena aleatoria de longitud 1024 que consta de caracteres hexadecimales. Luego creamos un objeto
BigInteger
llamadoresult
pasando la cadena al constructorBigInteger(String)
. Finalmente, imprimimos elBigInteger
tanto en formato hexadecimal como decimal usando el métodoToString()
.¡Espero que esto ayude! Avísame si tienes alguna otra pregunta.</time.h></stdlib.h>