Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 296
xtensa1408 Avatar asked Dec 06 '22 00:12

xtensa1408


2 Answers

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.

like image 112
Bathsheba Avatar answered Dec 23 '22 06:12

Bathsheba


With Boost:

boost::lexical_cast<std::string>(myVar);

C++98:

std::ostringstream stream;
stream << myVar;
stream.str();
like image 43
John Zwinck Avatar answered Dec 23 '22 06:12

John Zwinck