digitalmars.D.learn - Convert hex to binary
- nrgyzer (7/7) Apr 24 2015 Hi,
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (28/35) Apr 24 2015 Here is one way:
- Steven Schveighoffer (13/20) Apr 24 2015 import std.conv : parse;
- Jesse Phillips (8/15) Apr 24 2015 import std.stdio;
- nrgyzer (5/24) Apr 24 2015 Thanks to all of you for the solutions, but what if the
- Steven Schveighoffer (4/31) Apr 24 2015 Well, technically, a hex string can be split on 16-character boundaries,...
- Ivan Kazmenko (19/27) Apr 24 2015 BigInt can be constructed from a decimal string:
- Rene Zwanenburg (3/33) Apr 24 2015 ATM BigInt already supports hex strings; it looks for a 0x
- Rene Zwanenburg (4/38) Apr 24 2015 Or use BigInt:
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
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
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
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
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: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?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
On 4/24/15 2:50 PM, nrgyzer wrote:On Friday, 24 April 2015 at 18:45:55 UTC, Jesse Phillips wrote:Well, technically, a hex string can be split on 16-character boundaries, and then you could parse each one. -SteveOn Friday, 24 April 2015 at 18:14:07 UTC, nrgyzer 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?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
On Friday, 24 April 2015 at 18:55:07 UTC, Steven Schveighoffer wrote: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.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
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:ATM BigInt already supports hex strings; it looks for a 0x prefix. A radix parameter would be nice, but this works today ;)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.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
On Friday, 24 April 2015 at 18:55:07 UTC, Steven Schveighoffer wrote:On 4/24/15 2:50 PM, nrgyzer wrote:Or use BigInt: http://dlang.org/phobos/std_bigint.htmlOn Friday, 24 April 2015 at 18:45:55 UTC, Jesse Phillips wrote:Well, technically, a hex string can be split on 16-character boundaries, and then you could parse each one. -SteveOn Friday, 24 April 2015 at 18:14:07 UTC, nrgyzer 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?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