Convert hex to binary

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


On Friday, 24 April 2015 at 19:15:04 UTC, Ivan Kazmenko wrote:
> On Friday, 24 April 2015 at 18:55:07 UTC, Steven Schveighoffer 
> wrote:
>>> 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
>
> BigInt can be constructed from a decimal string:
>
> -----
> import std.bigint, std.conv, std.stdio, std.string;
> void main(){readln.strip.to!BigInt.writeln;}
> -----
>
> The same could have been done in the library for function "to" 
> accepting the second argument, like this:
>
> -----
> import std.bigint, std.conv, std.stdio, std.string;
> void main(){readln.strip.to!BigInt(16).writeln;}
> -----
>
> It seems trivial technically, but I wonder if there's some 
> library design drawback.  After all, to!BigInt from the default 
> base 10 is the same O(n^2) as to!BigInt from a variable base, 
> so it's not like the function is going to hide complexity more 
> than it already does.
>
> Ivan Kazmenko.

ATM BigInt already supports hex strings; it looks for a 0x 
prefix. A radix parameter would be nice, but this works today ;)


More information about the Digitalmars-d-learn mailing list