Convert a hex string into a ubyte[] or OutBuffer

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 19 04:51:54 PDT 2014


Darren:

> Let's say I have a hex representation of a large number:
>
> String hexnum = "16D81B16E091F31BEF";
>
> I'd like to convert it into a ubyte[]

A simple way is to use hex strings and then cast it to 
immutable(ubyte)[]:

void main() {
     immutable hexNum = 
cast(immutable(ubyte)[])x"16D81B16E091F31BEF";
}

The immutable at the cast point is needed because string literals 
are immutable in D. If you need a mutable ubyte[] you need a dup.

Also vote here:
https://issues.dlang.org/show_bug.cgi?id=5909

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list