【问题标题】:How to convert CryptoPP::Integer to char*如何将 CryptoPP::Integer 转换为 char*
【发布时间】:2014-09-18 05:38:58
【问题描述】:

我想将 myVar 从 CryptoPP:Integer 转换为 char* 或 String : 代码如下:

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

我一直在互联网上搜索将CryptoPP:Integer 转换为char*,但我没有找到运气。所以,要么将CryptoPP:Integer 转换为char* 确实是所有人的问题,要么我不太了解在C++ 中键入CryptoPP:Integer

有人可以帮帮我吗?

【问题讨论】:

  • char* 是 C 而不是 C++ 纯粹主义者会说。你真的想摆弄char*char 的数组吗?试试snprintf
  • @harper:建议 *printf() 是合理的,但应避免设计不安全的 sprintf() 以支持 snprintf()
  • 我的代码中没有任何内容可以让我们说“整数包含内存地址”。如果你愿意,我会发布所有代码!

标签: c++ visual-c++ crypto++


【解决方案1】:

一种方法是使用std::stringstream

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

使用std::string s = ss.str(); 提取底层std::string。然后,只要s 在范围内,您就可以使用s.c_str() 访问const char* 缓冲区。一旦您调用并依赖c_str() 的结果,就不要以任何方式更改s,因为这样做并随后依赖该结果的行为是未定义

有更简洁的 C++11 解决方案,但这需要您(和我)更多地了解该类型。

【讨论】:

  • +1 确实支持std::ostream 转储。 Use The Source, Luke (line 3371).
  • “这需要你(和我)更多地了解该类型” - Google 可以提供帮助:cryptopp.com/docs/ref/class_integer.html(并确认它是可流式传输的,并且似乎没有“更整洁”的方式)。
  • 当然,假设 CryptoPP 就是其中之一。 OP没有向我们透露这一点。是的,我知道我在这里接近于猫科动物。
  • @Bathsheba:确实。 crypto++ 标签为这一假设提供了一些支持。
【解决方案2】:

使用增强:

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

C++98:

std::ostringstream stream;
stream << myVar;
stream.str();

【讨论】:

  • 应该是strlen(str)-1 而不是sizeof(str);在C 示例中
  • itoa,ltoa 来自标准库?
  • std::to_stringsnprintf 都不适用于任意类类型。流式传输(直接或通过lexical_cast)应该可以工作。
  • @Ulterior:不,C 库中没有任何东西可以将 C++ 类类型转换为字符串。
  • @xtensa1408 - to_string is not a member of std 可能来自 Boost,而不是 Crypto++。该库不能提供 to_stringInteger 特化,因为它不是模板函数。并且该库无法将符号添加到 std 命名空间。另见Is specialization of std::to_string for custom types allowed by the C++ standard?
【解决方案3】:

有几种不同的方法可以做到这一点,这取决于你想要什么。 char* 在这种情况下没有提供足够的信息。

这是使用插入运算符时得到的结果:

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;

结果:

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

但是,如果您想获取原始字符串“hello”(回复:您的 Raw RSA 项目):

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;

结果:

$ ./cryptopp-test.exe
Str: Hello

但是,如果您只想在 Integer 中使用字符串,那么:

Integer i("11111111111111111111");    
ostringstream oss;

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

cout << str << endl;

结果:

$ ./cryptopp-test.exe
1111111111111111111.

【讨论】:

    【解决方案4】:

    如果CryptoPP::Integer 可以发送到std::cout 之类的输出流(正如您的代码所建议的那样),那么您可以使用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();
    }
    

    然后,一旦你有一个 std::string 实例,你可以使用 std::string::c_str() 将它转换为 const char*
    (但我认为在 C++ 代码中,您应该使用 safe 字符串类,例如 std::string,而不是原始 C 风格的字符指针)。


    PS
    我假设CryptoPP::Integer 不是int 的微不足道的typedef。
    如果您想将int 转换为std::string,那么您可能只想使用C++11 的std::to_string()

    【讨论】:

      猜你喜欢
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多