Convert a hex string into a ubyte[] or OutBuffer
Darren via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 19 10:06:12 PDT 2014
On Monday, 19 May 2014 at 12:28:14 UTC, anonymous wrote:
> On Monday, 19 May 2014 at 11:36:43 UTC, Darren wrote:
>>
>> 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();
Nice, thanks! I've added a slightly edited version as the answer
to this question on stackoverflow:
http://stackoverflow.com/a/23741556/47481
More information about the Digitalmars-d-learn
mailing list