Search code examples
c++encodingcryptographycrypto++

Convert Hex string to bytes in Crypto++


I have string of hexadecimals which I need to convert to const byte*. I am using Crypto++ to do hashing and it needs the key to be in const byte* Is there any way i can convert the string of hexadecimal into const byte* using any of the Crypto++ libs or must i need to come up with my own?


Solution

  • There is a HexDecoder class in Crypto++.

    You need to feed this characters. It seems that Crypto++ does not directly distinguish between characters and bytes. So the following line of code supplied by varren will work:

    string destination;
    StringSource ss(source, true, new HexDecoder(new StringSink(destination)));    
    const byte* result = (const byte*) destination.data();