Thread overview
Convert hex to binary
Apr 24, 2015
nrgyzer
Apr 24, 2015
Ali Çehreli
Apr 24, 2015
Jesse Phillips
Apr 24, 2015
nrgyzer
Apr 24, 2015
Ivan Kazmenko
Apr 24, 2015
Rene Zwanenburg
Apr 24, 2015
Rene Zwanenburg
April 24, 2015
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!
April 24, 2015
On 04/24/2015 11:14 AM, 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!

Here is one way:

import std.stdio;
import std.format;

string hexToBin(string source)
{
    ulong value;
    formattedRead(source, "%x", &value);
    return format("%b", value);
}

enum string hex = "hex";

string decode(string TargetFormat)(string source)
{
    static if (TargetFormat == "hex") {
        return hexToBin(source);

    } else {
        static assert(false,
                      format("I don't know how to decode to '%s'",
                             TargetFormat));
        return "";
    }
}

void main()
{
    assert("123456789ABCDEF".decode!hex ==
           "100100011010001010110011110001001101010111100110111101111");
}

Ali

April 24, 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

April 24, 2015
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));
    }
April 24, 2015
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?
April 24, 2015
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
April 24, 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.
April 24, 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
April 24, 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 ;)