Convert a hex string into a ubyte[] or OutBuffer

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 19 05:28:14 PDT 2014


On Monday, 19 May 2014 at 11:36:43 UTC, Darren wrote:
> String hexnum = "16D81B16E091F31BEF";

string (lowercase)

> I'd like to convert it into a ubyte[] in order to Base64 encode 
> it (or, indeed ASCII85 or Base32).
>
> eg, [16, D8, 1B, 16, E0, 91, F3, 1B, EF]
>
> Is there an idiomatic/simple way to do that?

import std.conv: parse;
import std.array: array;
import std.range: chunks;
import std.algorithm: map;

ubyte[] bytes = hexnum /* "16D8..." */
      .chunks(2) /* "16", "D8", ... */
      .map!(twoDigits => twoDigits.parse!ubyte(16)) /* 0x16, 0xD8,
... */
      .array();


More information about the Digitalmars-d-learn mailing list