HexDecoder output is empty

I have a problem with cryptopp562 (on Debian for which it matters) I have a hex string and I am trying to convert it to a decimal int. I am using HexDecoder in cryptopp (as I am already using cryptopp for other things in the project). Since I don't know how to go from a hexadecimal string to a decimal int in one step, I have an intermediate decimal string step. So he goes

Hex string> Decimal string> Decimal int

However, my pipeline seems to be wrong, but I can't for the life of me figure out why. I don't even get a hex string to the decimal line to the right, so my decimal int reads 0 all the time. I've used Base64Encoder (and Decoder) and ZlibCompressor (and Decompressor) in the past with no problem, so it's kind of a little confusing because it should be just the same.

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*/

      

But as I said, after running, RecoveredDecimalString.empty () returns true. At first I thought it was because I missed all the pump parameters, but adding that it didn't make any difference, nothing is leaking anyway.

A similar question was asked (and answered) a year ago . The answer came back as "read the cryptoPP wiki" but I can't see how my code is different from what is on their wiki.

What have I forgotten? I know it will be something very small.

+3


source to share


1 answer


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*/

      

Name yours StringSource

. In updating your code, notice what StringSource

was named 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*/

      



Some versions of GCC have problems with anonymous declarations. A while ago I traced it down to StringSink

destructors running too early (before the data was pumped in). I wanted to write a GCC bug report, but I could never reduce it to a minimum.

You can also do:

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

      

+1


source







All Articles