5

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?

3
  • char* is C and not C++ will purists say. Do you really want to fiddle with char* and array of char? Try snprintf.
    – harper
    Sep 17, 2014 at 10:21
  • 1
    @harper: Suggesting *printf() is reasonable, but the unsafe-by-design sprintf() should be avoided in favor of snprintf(). Sep 17, 2014 at 10:22
  • There is nothing in my code that allow us to say "the integer holds a memory adress". If you want I will publish all the code !
    – xtensa1408
    Sep 17, 2014 at 10:24

4 Answers 4

5

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.

4
  • +1 indeed it does support std::ostream dumping. Use The Source, Luke (line 3371).
    – WhozCraig
    Sep 17, 2014 at 10:27
  • 1
    "that requires you (and me) to know more about the type" - Google can help with that: cryptopp.com/docs/ref/class_integer.html (and confirms that it is streamable, and there doesn't seem to be any "neater" way). Sep 17, 2014 at 10:29
  • Assuming, of course, that CryptoPP is one of those things. OP doesn't reveal that to us. Yes, I know I'm bordering on feline pedantry here.
    – Bathsheba
    Sep 17, 2014 at 10:29
  • @Bathsheba: Indeed. The crypto++ tag lends some weight to that assumption. Sep 17, 2014 at 10:32
5

With Boost:

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

C++98:

std::ostringstream stream;
stream << myVar;
stream.str();
8
  • that should be strlen(str)-1 instead of sizeof(str); in C example Sep 17, 2014 at 10:25
  • itoa,ltoa from stdlib?
    – Ulterior
    Sep 17, 2014 at 10:26
  • Neither std::to_string nor snprintf will work with arbitrary class types. Streaming (directly or through lexical_cast) should work though. Sep 17, 2014 at 10:26
  • @Ulterior: No, there's nothing in the C library that can convert a C++ class type to a string. Sep 17, 2014 at 10:28
  • 1
    @xtensa1408 - The to_string is not a member of std is likely coming from Boost, and not Crypto++. The library cannot provide an Integer specialization of to_string because it not a template function. And the library cannot add symbols to the std namespace. Also see Is specialization of std::to_string for custom types allowed by the C++ standard?
    – jww
    Feb 24, 2017 at 23:08
3

There's a few different ways you can do this, depending on what you want. char* does not provide enough information in this case.

Here's what you get when using the insertion 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;

That results in:

$ ./cryptopp-test.exe
Oct: 4414533066157o
Dec: 310939249775.
Hex: 48656c6c6fh

However, if you want to get the original string "hello" (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;

That results in:

$ ./cryptopp-test.exe
Str: Hello

If, however, you just want the string used in an Integer, then:

Integer i("11111111111111111111");    
ostringstream oss;

oss << i;    
string str = oss.str();

cout << str << endl;

That results in:

$ ./cryptopp-test.exe
1111111111111111111.
2

If CryptoPP::Integer can be sent to output streams like std::cout (as your code seems to suggest), then 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 a std::string instance, you can convert it to const char* using std::string::c_str().
(But I think in C++ code you should use a safe string class like std::string in general, instead of raw C-style character pointers).


PS
I'm assuming CryptoPP::Integer is not a trivial typedef for an int.
If you want to convert an int to a std::string, then you may want to just use C++11's std::to_string().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.