Logo

Programming-Idioms

History of Idiom 176 > diff from v8 to v9

Edit summary for version 9 by cym13:
New D implementation by user [cym13]

Version 8

2019-02-11, 20:01:36

Version 9

2019-03-19, 16:32:07

Idiom #176 Hex string to byte array

From hex string s of 2n digits, build the equivalent array a of n bytes.
Each pair of hexadecimal characters (16 possible values per digit) is decoded into one byte (256 possible values).

Idiom #176 Hex string to byte array

From hex string s of 2n digits, build the equivalent array a of n bytes.
Each pair of hexadecimal characters (16 possible values per digit) is decoded into one byte (256 possible values).

Extra Keywords
hexa
Extra Keywords
hexa
Imports
import std.algorithms;
import std.array;
import std.conv;
import std.range;
Code
ubyte[] a = s.chunks(2)
             .map!(digits => digits.to!ubyte)
             .array;
Comments bubble
Take 2 digits at a time and convert them to ubytes. Map is lazy so we use array to actually build a list.