Convert hex to binary

Ivan Kazmenko via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 24 12:15:03 PDT 2015


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.


More information about the Digitalmars-d-learn mailing list