Is there a more elegant way to do this in D?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Apr 8 20:41:40 UTC 2021


On Thu, Apr 08, 2021 at 08:28:44PM +0000, Alain De Vos via Digitalmars-d-learn wrote:
> The ascii code of 0 is 48 so I think you can add everywhere 48 (but
> I'm not a specialist)

Why bother with remembering it's 48? Just add '0', like this:

	int a = [1, 0, 1, 0, 1, ...];
	string s = a.map!(i => cast(char)(i + '0')).array;
	writeln(s);

Or better yet, if you just want to output it and don't need to store the
array, just use the range directly:

	int a = [1, 0, 1, 0, 1, ...];
	auto r = a.map!(i => cast(char)(i + '0'));
	writeln(r);


T

-- 
People say I'm arrogant, and I'm proud of it.


More information about the Digitalmars-d-learn mailing list