HexDecoder输出为空

时间:2014-08-15 13:15:53

标签: c++ hex pipeline crypto++

我对cryptopp562有点问题(在Debian上有什么问题)我有一个十六进制字符串,我试图将它转换为十进制int。我在cryptopp中使用HexDecoder(因为我已经在项目的其他方面使用了cryptopp)。由于我不知道如何在一步中直接从十六进制字符串转换为十进制int,我有一个十进制字符串的中间步骤。所以它就是

十六进制字符串>十进制字符串>十进制int

然而我的管道似乎不正确,但我不能为我的生活找出原因。我甚至没有得到十六进制字符串右边的十六进制字符串,所以我的十进制int只是不断读取0.我曾经使用过Base64Encoder(和Decoder)和ZlibCompressor(和Decompressor),没有问题,所以这是有点尴尬,因为它应该更加相同。

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。起初我以为是因为我错过了泵的所有参数,但添加没有任何区别,仍然没有任何流动。

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

我忘记了什么?我知道它会变得非常小。

1 个答案:

答案 0 :(得分:1)

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();