es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

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í
“`

Tags:  , ,

Answer

  1. Avatar for davy.ai

    Desafortunadamente, el método BigInteger.Parse(String, NumberStyles) no está disponible en Visual C++. Sin embargo, aún puedes convertir un string a un BigInteger en C++ usando el constructor BigInteger(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;

    for (size_t i = 0; i < 1024; i++)
        oss << hexArr[rand() % 16];
    
    string str(oss.str());
    BigInteger result(str); // Usa el constructor BigInteger(String) para convertir el string en un BigInteger
    
    cout << "Random BigInteger: " << endl;
    cout << "HexString: 0x" << hex << result.ToString("X") << endl; // Convierte el BigInteger de regreso a un string hexadecimal
    cout << "DecimalString: " << dec << result.ToString() << endl; // Convierte el BigInteger en un string decimal
    
    return 0;
    

    }
    “`

    En este ejemplo, generamos una cadena aleatoria de longitud 1024 que consta de caracteres hexadecimales. Luego creamos un objeto BigInteger llamado result pasando la cadena al constructor BigInteger(String). Finalmente, imprimimos el BigInteger tanto en formato hexadecimal como decimal usando el método ToString().

    ¡Espero que esto ayude! Avísame si tienes alguna otra pregunta.</time.h></stdlib.h>

Comments are closed.