Convert hex to binary

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 24 11:43:58 PDT 2015


On 4/24/15 2:14 PM, nrgyzer wrote:
> Hi,
>
> I'm looking for a function that converts my hex-string to a binary
> representation. In Python I write the following:
>
> myHex = "123456789ABCDEF"
> myBin = myHex.decode('hex')
>
> But how to do the same in D? Is there any function?
>
> Thanks for suggestions!

import std.conv : parse;
import std.stdio;

void main()
{
     auto myHex = "123456789ABCDEF";
     auto myBin = parse!ulong(myHex, 16);
     writeln(myBin); // 81985529216486895
}

Note, python may make arbitrary long integers, but D you must specify 
the size for your integer to the parse function. In this case, you need 
ulong which is 64 bits.

-Steve



More information about the Digitalmars-d-learn mailing list