On Tuesday, 6 February 2018 at 18:33:02 UTC, Ralph Doncaster wrote:
I've been reading std.conv and std.range, trying to figure out a high-level way of converting a hex string to bytes. The only way I've been able to do it is through pointer access:

import std.stdio;
import std.string;
import std.conv;

void main()
{
    immutable char* hex = "deadbeef".toStringz;
    for (auto i=0; hex[i]; i += 2)
        writeln(to!byte(hex[i]));
}

Thanks for all the feedback. I'll have to do some more reading about maps. My initial though is they don't seem as readable as loops.
The chunks() is useful, so for now what I'm going with is:
    ubyte[] arr;
    foreach (b; "deadbeef".chunks(2))
    {
        arr ~= b.to!ubyte(16);
    }

Reply via email to