Search code examples
c++visual-c++crypto++

How to convert CryptoPP::Integer to char*


I want to convert myVar from CryptoPP:Integer to char* or to String : The code is below :

CryptoPP::Integer myVar = pubKey.ApplyFunction(m);
std::cout << "result: " << std::hex << myVar<< std::endl;

I have been searching the Internet for converting CryptoPP:Integer to char* but I have had no luck finding. So, either it is really a problem with all to convert CryptoPP:Integer to char*, either I didn't understand very well type CryptoPP:Integer within C++ .

Can someone help me please?


Solution

  • One way, without knowing much more about CryptoPP::Integer other than it clearly supports << as implied by your question, is to use std::stringstream:

    std::stringstream ss;
    ss << std::hex /*if required*/ << myVar;
    

    Extract the underlying std::string using, say std::string s = ss.str();. You can then use s.c_str() to access the const char* buffer for as long as s is in scope. Don't change s in any way once you've called and relied upon the result of c_str() as the behaviour of doing so and subsequently relying on that result is undefined.

    There are neater C++11 solutions but that requires you (and me) to know more about the type.