View source code
Display the source code in std/digest/package.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aBugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinglocal clone.

Function std.digest.fromHexString

Converts a hex text string to a range of bytes.

ubyte[] fromHexString(String)(
  String hex
) pure @safe
if (isSomeString!String);

Parameters

NameDescription
hex String representation of a hexdecimal-encoded byte array.

Returns

An newly allocated array of bytes.

Throws

Exception on invalid input.

Example

ubyte[] dby  = "0xBA".fromHexString;

See Also

std.digest.fromHexString for a range version of the function.

Example

// Single byte
writeln("0xff".fromHexString); // [255]
writeln("0xff"w.fromHexString); // [255]
writeln("0xff"d.fromHexString); // [255]
writeln("0xC0".fromHexString); // [192]
writeln("0x00".fromHexString); // [0]

// Nothing
writeln("".fromHexString); // []
writeln(""w.fromHexString); // []
writeln(""d.fromHexString); // []

// Nothing but a prefix
writeln("0x".fromHexString); // []
writeln("0x"w.fromHexString); // []
writeln("0x"d.fromHexString); // []

// Half a byte
writeln("0x1".fromHexString); // [0x01]
writeln("0x1"w.fromHexString); // [0x01]
writeln("0x1"d.fromHexString); // [0x01]

// Mixed case is fine.
writeln("0xAf".fromHexString); // [0xAF]
writeln("0xaF".fromHexString); // [0xAF]

// Multiple bytes
writeln("0xfff".fromHexString); // [0x0F, 0xFF]
writeln("0x123AaAa".fromHexString); // [0x01, 0x23, 0xAA, 0xAA]
writeln("EBBBBF".fromHexString); // [0xEB, 0xBB, 0xBF]

// md5 sum
assert("d41d8cd98f00b204e9800998ecf8427e".fromHexString == [
    0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
    0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E,
]);

Example

// Cycle self-test
const ubyte[] initial = [0x00, 0x12, 0x34, 0xEB];
writeln(initial); // initial.toHexString().fromHexString()

Authors

Johannes Pfau

License

Boost License 1.0.