www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Convert hex to binary

reply "nrgyzer" <nrgyzer gmail.com> writes:
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!
Apr 24 2015
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Apr 24 2015
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Apr 24 2015
prev sibling parent reply "Jesse Phillips" <Jessekphillips+D gmail.com> writes:
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)); }
Apr 24 2015
parent reply "nrgyzer" <nrgyzer gmail.com> writes:
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?
Apr 24 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Apr 24 2015
next sibling parent reply "Ivan Kazmenko" <gassa mail.ru> writes:
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.
Apr 24 2015
parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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 ;)
Apr 24 2015
prev sibling parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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
Apr 24 2015