How to convert CryptoPP :: Integer to char *
I want to convert myVar from CryptoPP:Integer
to char*
or to String: Code below:
CryptoPP::Integer myVar = pubKey.ApplyFunction(m);
std::cout << "result: " << std::hex << myVar<< std::endl;
I searched the web for converting CryptoPP:Integer
to char*
, but I had no luck finding. So either this is really a problem with everyone to convert CryptoPP:Integer
to char*
, or I don't understand type very well CryptoPP:Integer
in C ++.
Can anyone help me?
source to share
In one case, not knowing much more about CryptoPP::Integer
other than what it explicitly supports <<
, as implied by your question, one should use std::stringstream
:
std::stringstream ss;
ss << std::hex /*if required*/ << myVar;
Extract the base std::string
one using, say std::string s = ss.str();
. Then you can use s.c_str()
to access the buffer const char*
as long as it s
is in scope. Do not change in s
any way as soon as you call and rely on the result c_str()
, as the behavior of this and subsequently rely on this result: undefined.
There are simpler C ++ 11 solutions, but this requires you (and me) to learn more about type.
source to share
If CryptoPP::Integer
it is possible to send to output streams like std::cout
(as your code suggests) you can use : std::ostringstream
#include <sstream> // For std::ostringstream
....
std::string ToString(const CryptoPP::Integer& n)
{
// Send the CryptoPP::Integer to the output stream string
std::ostringstream os;
os << n;
// or, if required:
// os << std::hex << n;
// Convert the stream to std::string
return os.str();
}
Then, once you have an instance std::string
, you can convert it to const char*
with .
(But I think in C ++ code you should use a safe string class like in general instead of raw C-style character pointers). std::string::c_str()
std::string
PS
I'm guessing it is CryptoPP::Integer
not a trivial typedef for int
.
If you want to convert int
to std::string
, then you can just use C ++ 11 . std::to_string()
source to share
There are several different ways to do this, depending on what you want. char*
does not provide sufficient information in this case.
This is what you get when using the insert operator:
byte buff[] = { 'H', 'e', 'l', 'l', 'o' };
CryptoPP::Integer n(buff, sizeof(buff));
cout << "Oct: " << std::oct << n << endl;
cout << "Dec: " << std::dec << n << endl;
cout << "Hex: " << std::hex << n << endl;
This leads to:
$ ./cryptopp-test.exe
Oct: 4414533066157o
Dec: 310939249775.
Hex: 48656c6c6fh
However, if you want to get the original "hello" string (re: your Raw RSA project):
byte buff[] = { 'H', 'e', 'l', 'l', 'o' };
CryptoPP::Integer n(buff, sizeof(buff));
size_t len = n.MinEncodedSize();
string str;
str.resize(len);
n.Encode((byte *)str.data(), str.size(), Integer::UNSIGNED);
cout << "Str: " << str << endl;
This leads to:
$ ./cryptopp-test.exe
Str: Hello
If, however, you just want the string used in Integer
, then:
Integer i("11111111111111111111");
ostringstream oss;
oss << i;
string str = oss.str();
cout << str << endl;
This leads to:
$ ./cryptopp-test.exe
1111111111111111111.
source to share