BDE 4.14.0 Production release
|
Provide mechanism for encoding text into hexadecimal.
This component provides a class, bdlde::HexEncoder
, for encoding plain text into its hexadecimal representation.
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 using a bdlde::HexEncoder
object to encode bytes into a hexidecimal format. For dependency reasons, a more complete example, showing both encoding and decoding can be found in bdlde_hexdecoder .
In the example below, we implement a function streamEncoder
, that reads text from bsl::istream
, encodes that text into hex representation, and writes the encoded text to a bsl::ostream
. streamEncoder
returns 0 on success and a negative value if the input data could not be successfully encoded 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::HexEncoder
:
And write encoded text to the output stream:
Then, we need to store the unhandled symbol (if there is one) to the output buffer and complete the work of our encoder:
Next, to demonstrate how our function works we need to create a stream with data to encode. Assume that we have some character buffer, BLOOMBERG_NEWS
, and a function, streamDecoder
mirroring the work of the streamEncoder
. Below we should encode this string into a hexidecimal format:
Then, we use our function to encode text:
This example does not decode the resulting hexidecimal text, for a more complete example, see bdlde_hexdecoder .