Convert hex to binary

Rene Zwanenburg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 24 12:15:14 PDT 2015


On Friday, 24 April 2015 at 18:55:07 UTC, Steven Schveighoffer 
wrote:
> On 4/24/15 2:50 PM, nrgyzer wrote:
>> On Friday, 24 April 2015 at 18:45:55 UTC, Jesse Phillips wrote:
>>> On Friday, 24 April 2015 at 18:14:07 UTC, 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.stdio;
>>>    void main(){
>>>        import std.conv;
>>>        import std.format;
>>>
>>>        auto i = to!ulong("123456789ABCDEF", 16);
>>>
>>>        writeln(format("%b", i));
>>>    }
>>
>> Thanks to all of you for the solutions, but what if the 
>> hex-string
>> exceeds the limit of ulong, for instance
>> "123456789ABCDEF0123456789ABCDEF1234". How to convert them to a
>> ulong-array?
>
> Well, technically, a hex string can be split on 16-character 
> boundaries, and then you could parse each one.
>
> -Steve

Or use BigInt:

http://dlang.org/phobos/std_bigint.html


More information about the Digitalmars-d-learn mailing list