digitalmars.D - Decimal (String) to Binary?
- MDtox (1/1) Oct 18 2018 How to convert decimal and string to binary?
- H. S. Teoh (21/22) Oct 18 2018 What exactly do you mean by "binary"? If you mean convert a string to a
On Thu, Oct 18, 2018 at 07:26:08PM +0000, MDtox via Digitalmars-d wrote:How to convert decimal and string to binary?What exactly do you mean by "binary"? If you mean convert a string to a numerical type, use std.conv.to: import std.conv : to; auto x = "12345"; auto i = x.to!int; assert(i == 12345); If you want to print a decimal string in binary: import std.stdio; auto x = "12345"; auto i = x.to!int; writefln("%b", i); // prints: 11000000111001 Or if you want the binary representation in string form: import std.format : format; auto x = "12345"; auto i = x.to!int; auto s = format("%b", i); assert(s == "11000000111001"); T -- Let's not fight disease by killing the patient. -- Sean 'Shaleh' Perry
Oct 18 2018