【发布时间】:2015-09-08 13:49:31
【问题描述】:
我必须在一个数据包中发送一个随机生成的数字 (rng),因此必须转换一个 SecByteBlock。我使用libcrafter 将rng 数组放入RawLayer 数据包中。
我关注了this answer;但它给我一个无效转换错误。
这是我使用的代码:
AutoSeededRandomPool prng;
SecByteBlock rng1;
prng.GenerateBlock( rng1, rng1.size() );
string rng1_str(reinterpret_cast<char const*>(rng1));
std::string rng1_str = std::string(rng1.data(), rng1.size());
Crafter::RawLayer number(rng1_str);
这两个想法都不行,都给我:
error: invalid conversion from ‘CryptoPP::AllocatorWithCleanup<unsigned char>::pointer {aka unsigned char*}’
to ‘const char*’ [-fpermissive]
std::string rng1_str = std::string(rng1.data(), rng1.size());
由于RawLayer 接受std::string 或const byte* 作为构造函数,我想将SecBlockByte 转换为这两种格式之一...
【问题讨论】:
-
尝试再次阅读错误消息,它会告诉您传递给
std::string构造函数的内容,以及构造函数所期望的内容。它们之间的差异非常小,可以通过简单的reinterpret_cast合理地解决。然而,更大的问题是您将如何使用 字符串对象。您会尝试将其用作文本吗?那么您可能会遇到问题,因为数据很可能不是文本。 -
我认为它需要
unsigned char *,但我试图用它重新解释演员阵容,但它也不起作用...... -
错误信息说你尝试传递
unsigned char *,而构造函数需要const char *。尝试投射到char*。 -
也没有用。我也尝试投到
unsigned char... -
现在你正在做一些不同的。问题中的错误信息明确指出
unsigned char *,即一个指针。
标签: c++ crypto++ libcrafter