Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Hex Encoder and Decoder using Crypto++

0.00/5 (No votes)
20 Nov 2000 1  
Encode binary data to and from hexadecimal format using the Crypto++ library

Introduction

The Crypto++ library is a freeware library of cryptographic schemes, written by Wei Dai. However the library also contains other useful classes which one is not made immediately aware of when you use the library. Two of these are the HexEncoder and HexDecoder classes which can be used to (surprise, surprise) encode and decode data to and from the hexadecimal text format e.g. 0-9,A-F.

Encoding

Encoding is very simple, say we had some data pData that was of length dwLen, that we wished to encode and store in pData2 which is of length dwLen*2, then

#include <hex.h> // from crypto++ library 

HexEncoder hexEncoder;
hexEncoder.Put(pData,dwLen);
hexEncoder.Close();
hexEncoder.Get(pData2,dwLen*2);

And there we have it. It is also possible to add multiple blocks of data to the encoders stream i.e.

HexEncoder hexEncoder;
hexEncoder.Put(pDataA,dwLenA);
hexEncoder.Put(pDataB,dwLenB);
hexEncoder.Put(pDataC,dwLenC);
hexEncoder.Close();
hexEncoder.Get(pData2,(dwLenA+dwLenB+dwLenC)*2);

Decoding

Decoding is equally simple.

HexDecoder hexDecoder;
hexDecoder.Put(pData,dwLen);
hexDecoder.Close();
hexDecoder.Get(pData2,dwLen/2); 

Remarks

Thanks to Wei Dai for his permission to write this article in what is hoped to be a series of articles on the use of his Crypto++ library.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here