Most performant way of converting int to string

Minas Mina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 23 23:53:05 PST 2015


On Wednesday, 23 December 2015 at 22:29:31 UTC, Andrew Chapman 
wrote:
> On Wednesday, 23 December 2015 at 11:46:37 UTC, Jakob Ovrum 
> wrote:
>> On Wednesday, 23 December 2015 at 11:21:32 UTC, Jakob Ovrum 
>> wrote:
>>> Dynamic memory allocation is expensive. If the string is 
>>> short-lived, allocate it on the stack:
>>
>> See also std.conv.toChars[1] for stringifying lazily/on-demand.
>>
>> http://dlang.org/phobos/std_conv#toChars
>
> Thanks Jakob!  I did try toChars but I couldn't quite figure 
> out a syntax of calling it that the compiler was happy with.  
> From memory I tried things along the lines of:
>
> string v = toChars!(16,char,LetterCase.lower)(i);
>
> to convert an integer to Hex for example, but the compiler 
> wasn't happy with it.  How would I convert an int to a string 
> using this?
>
> Cheers.

I haven't tested it, but:
`toChars` doesn't return a string -- that's the whole point :)
It returns a range, so you have to call it something like:
auto v = toChars!(16,char,LetterCase.lower)(i);


More information about the Digitalmars-d-learn mailing list