D: Convert/parse uint integer to string. (@nogc)

Nick Treleaven nick at geany.org
Mon Nov 27 13:26:25 UTC 2023


On Monday, 27 November 2023 at 12:34:30 UTC, Nick Treleaven wrote:
> On Friday, 24 November 2023 at 09:35:00 UTC, BoQsc wrote:
> You can use std.conv.toChars:
>
> ```d
> void main() @nogc
> {
>     int n = 515;
>
>     import std.conv;
>     char[10] s = 0;
>     auto r = n.toChars();
>     assert(r.length < s.length);
>     size_t i;
>     foreach (c; r)
>         s[i++] = c;
>
>     import core.stdc.stdio;
>     puts(s.ptr);
> }
> ```

Or, using std.experimental.allocator:

```d
void main() @nogc
{
     int n = 515;

     import std.conv;
     import std.experimental.allocator;
     import std.experimental.allocator.mallocator;
     alias a = Mallocator.instance;
     auto s = a.makeArray(n.toChars);
     assert(s == "515");
     a.dispose(s);
}
```


More information about the Digitalmars-d-learn mailing list