You could pass in a reference to a string for the output, then use 
out.c_str() after calling it, or if you want to avoid strings altogether, you 
can convert without Crypto++.

void crypt::hexDec(string data, string &out)
{
// Your code, except don't declare "string out".

}


or:


NOTE: this function assumes a null terminated pHex string, and assumes an even 
number of hex characters


int hex2bin(const char *pHex, unsigned char *pBin, int nMaxBinBytes)
{
   bool bIsHexChar;
   bool bIsUpper = true;
   unsigned char nTemp;
   unsigned char nOut;
   int nNumBinBytes = 0;

   while (*pHex && nNumBinBytes < nMaxBinBytes)
   {
      bIsHexChar = true;
      if (     *pHex >= '0' && *pHex <= '9') nTemp = *pHex - 48;
      else if (*pHex >= 'A' && *pHex <= 'F') nTemp = *pHex - 55;
      else if (*pHex >= 'a' && *pHex <= 'f') nTemp = *pHex - 87;
      else bIsHexChar = false;

      ++pHex;

      if (bIsHexChar)
      {
         if (bIsUpper)
         {
            nOut = (nTemp << 4) & 0xF0;
            bIsUpper = false;
         }
         else
         {
            nOut = (nOut | (nTemp & 0x0F));
            *pBin++ = nOut;
            ++nNumBinBytes;
            bIsUpper = true;
         }
      }
   }
   return(nNumBinBytes);
}






>________________________________
> From: Michael <[email protected]>
>To: Crypto++ Users <[email protected]> 
>Sent: Thursday, March 29, 2012 11:49 AM
>Subject: Hex To Byte?
> 
>this is currently my hex decoder
>
>string crypt::hexDec(string data)
>{
>    engine.pl("crypt-> generating plaintext from hex",1);
>
>    //create cipher
>    CryptoPP::HexDecoder decoder;
>
>    //create and write data to buffer
>    string out;
>    decoder.Attach(new CryptoPP::StringSink(out));
>    decoder.Put((byte*)engine.str2ch(data), data.length());
>    decoder.MessageEnd();
>
>    //return buffer
>    return out;
>}
>
>but the problem is now i need one to return as a char array, and
>because im dealing with binary files and such, cant exactly be
>converting from string and back.
>
>Whats the correct way to just convert it to and return a char array
>
>-- 
>You received this message because you are subscribed to the "Crypto++ Users" 
>Google Group.
>To unsubscribe, send an email to [email protected].
>More information about Crypto++ and this group is available at 
>http://www.cryptopp.com.
>
>
>

-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.

Reply via email to