tango.util.encode.Base16

License:

BSD style: see license.txt

Author:

Ulrik Mikaelsson

Standards:

rfc3548, rfc4648

This module is used to decode and encode hex char[] arrays.

Example:

1
2
3
4
5
6
7
8
char[] blah = "Hello there, my name is Jeff.";

scope encodebuf = new char[allocateEncodeSize(cast(ubyte[])blah)];
char[] encoded = encode(cast(ubyte[])blah, encodebuf);

scope decodebuf = new ubyte[encoded.length];
if (cast(char[])decode(encoded, decodebuf) == "Hello there, my name is Jeff.")
    Stdout("yay").newline;

Since v1.0
size_t allocateEncodeSize(const(ubyte[]) data)
calculates and returns the size needed to encode the length of the array passed.

Parameters:

dataAn array that will be encoded
size_t allocateEncodeSize(size_t length)
calculates and returns the size needed to encode the length passed.

Parameters:

lengthNumber of bytes to be encoded
char[] encode(const(ubyte[]) data, char[] buff)
encodes data and returns as an ASCII hex string.

Parameters:

datawhat is to be encoded
buffbuffer large enough to hold encoded data

Example:

1
2
3
char[512] encodebuf;
char[] myEncodedString = encode(cast(ubyte[])"Hello, how are you today?", encodebuf);
Stdout(myEncodedString).newline; // 48656C6C6F2C20686F772061726520796F7520746F6461793F
char[] encode(const(ubyte[]) data)
encodes data and returns as an ASCII hex string.

Parameters:

datawhat is to be encoded

Example:

1
2
char[] myEncodedString = encode(cast(ubyte[])"Hello, how are you today?");
Stdout(myEncodedString).newline; // 48656C6C6F2C20686F772061726520796F7520746F6461793F
ubyte[] decode(const(char[]) data)
decodes an ASCII hex string and returns it as ubyte[] data. Pre-allocates the size of the array.
This decoder will ignore non-hex characters. So: SGVsbG8sIGhvd yBhcmUgeW91IH RvZGF5Pw==

Is valid.

Parameters:

datawhat is to be decoded

Example:

1
2
char[] myDecodedString = cast(char[])decode("48656C6C6F2C20686F772061726520796F7520746F6461793F");
Stdout(myDecodeString).newline; // Hello, how are you today?
ubyte[] decode(const(char[]) data, ubyte[] buff)
decodes an ASCII hex string and returns it as ubyte[] data.
This decoder will ignore non-hex characters. So: SGVsbG8sIGhvd yBhcmUgeW91IH RvZGF5Pw==

Is valid.

Parameters:

datawhat is to be decoded
buffa big enough array to hold the decoded data

Example:

1
2
3
ubyte[512] decodebuf;
char[] myDecodedString = cast(char[])decode("48656C6C6F2C20686F772061726520796F7520746F6461793F", decodebuf);
Stdout(myDecodeString).newline; // Hello, how are you today?