c++ - 十六进制解码器输出为空

标签 c++ hex pipeline crypto++

我在使用 cryptopp562 时遇到了一些问题(在 Debian 上很重要)我有一个十六进制字符串,我正在尝试将其转换为十进制整数。我在 cryptopp 中使用 HexDecoder(因为我已经在项目中使用 cryptopp 进行其他操作)。由于我不知道如何一步直接从十六进制字符串转换为十进制 int,因此我有一个十进制字符串的中间步骤。就这样

十六进制字符串 > 十进制字符串 > 十进制整数

但是我的管道似乎不正确,但我终究无法弄清楚原因。我什至没有正确地将十六进制字符串转换为十进制字符串位,所以我的十进制 int 只是不断地读取 0。我过去使用过 Base64Encoder(和解码器)和 ZlibCompressor(和解压缩器)没有问题,所以这是有点尴尬,因为它应该更相似。

std::string RecoveredDecimalString;
std::string RecoveredHex = "39"; //Hex, so would be 63 in decimal
CryptoPP::StringSource (RecoveredHex, true /*PumpAll*/,
    new CryptoPP::HexDecoder(
        new CryptoPP::StringSink(RecoveredDecimalString) /*StringSink*/
    )/*HexDecoder*/
);/*StringSource*/

但就像我说的,运行后,RecoveredDecimalString.empty() 返回 true。起初我以为是因为我错过了 pump all 参数,但添加它没有任何区别,仍然没有任何流动。

A similar question was asked (and answered) a year ago .返回的答案是“阅读 cryptoPP wiki”,但我看不出我的代码与他们 wiki 上的代码有何不同。

我忘记了什么?我知道这将是非常小的事情。

最佳答案

std::string RecoveredDecimalString;
std::string RecoveredHex = "39"; //Hex, so would be 63 in decimal
CryptoPP::StringSource (RecoveredHex, true /*PumpAll*/,
    new CryptoPP::HexDecoder(
        new CryptoPP::StringSink(RecoveredDecimalString) /*StringSink*/
    )/*HexDecoder*/
);/*StringSource*/

命名您的 StringSource。在您的代码更新中,请注意 StringSource 被命名为 ss

std::string decoded;
std::string encoded = "39"; //Hex, so would be 63 in decimal
CryptoPP::StringSource ss(encoded, true /*PumpAll*/,
    new CryptoPP::HexDecoder(
        new CryptoPP::StringSink(decoded) /*StringSink*/
    )/*HexDecoder*/
);/*StringSource*/

某些版本的 GCC 在匿名声明方面存在问题。我在一段时间前追踪到 StringSink 析构函数运行得太快(在数据被抽取之前)。我想提交一份 GCC 错误报告,但我永远无法将其简化为最小的案例。

您还可以执行:

std::string decoded;
std::string encoded = "39"; //Hex, so would be 63 in decimal

CryptoPP::HexDecoder decoder(new CryptoPP::StringSink(decoded));
decoder.Put(encoded.data(), encoded.size());
decoder.MessageEnd();

关于c++ - 十六进制解码器输出为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25327066/

相关文章:

Jenkins pipelineJob DSL 不解释管道脚本中的变量

apache-spark - 读取自定义 pyspark 转换器

c++ - 如何在同一解决方案下为多个项目声明公共(public)#define?

python - 如何通过 cython 将 numpy 数组列表传递给 C++

c++ - 在复制节点后删除经典节点结构时,我们是否必须担心可能的覆盖?

Python unhexlify 十六进制到 alpha 转换失败

python - 如何使用 XGboost 针对不同的 `eval_metric` 优化 sklearn 管道?

c++ - 在 C++ 代码中奇怪地使用 void

c - 带十六进制负值的 sscanf

iOS 十六进制加法