BDE 4.14.0 Production release
|
Provide mechanism for decoding text from hexadecimal.
This component provides a class, bdlde::HexDecoder
, for decoding hexadecimal representation into plain text.
bdlde::HexEncoder
and bdlde::HexDecoder
provide a pair of template functions (each parameterized separately on both input and output iterators) that can be used respectively to encode and to decode byte sequences of arbitrary length into and from the printable Hex representation.
Each instance of either the encoder or decoder retains the state of the conversion from one supplied input to the next, enabling the processing of segmented input – i.e., processing resumes where it left off with the next invocation on new input. Instance methods are provided for both the encoder and decoder to (1) assert the end of input, (2) determine whether the input so far is currently acceptable, and (3) indicate whether a non-recoverable error has occurred.
The data stream is processed one byte at a time from left to right. Each byte
is segmented into two intermediate 4-bit quantities.
Each 4-bit quantity is in turn used as an index into the following character table to generate an 8-bit character.
Depending on the settings encoder represents values from 10 to 15 as uppercase (A
-F
) or lowercase letters(a
-f
).
Input values of increasing length along with their corresponding Hex encodings are illustrated below:
The data stream is processed two bytes at a time from left to right. Each sequence of two 8-bit quantities
is segmented into four intermediate 4-bit quantities.
The second and forth chunks are combined to get the resulting 8-bit character.
Whitespace characters are ignored. On any non-alphabet character the decoder reports an error. In order for a Hex encoding to be valid the length of the input data (excluding any whitespace characters) must be a multiple of two.
Input values of increasing length along with their corresponding Hex encodings are illustrated below (note that the encoded whitespace character is skipped and the resulting string does not contain it):
This section illustrates intended use of this component.
The following example shows how to use a bdlde::HexDecoder
object to implement a function, streamDecoder
, that reads hex representation from bsl::istream
, decodes that text, and writes the decoded text to a bsl::ostream
. streamDecoder
returns 0 on success and a negative value if the input data could not be successfully decoded or if there is an I/O error.
First we create an object, create buffers for storing data, and start loop that runs while the input stream contains some data:
On each iteration we read some data from the input stream:
Convert obtained text using bdlde::HexDecoder
:
And write decoded text to the output stream:
Then we need to complete the work of our decoder:
Next, to demonstrate how our function works we need to create a stream with encoded data. Assume that we have some character string, BLOOMBERG_NEWS
, and a function, streamEncoder
mirroring the work of the streamDecoder
:
Now, we use our function to decode text:
Finally, we observe that the output fully matches the original text: