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

Nick Treleaven nick at geany.org
Mon Nov 27 12:34:30 UTC 2023


On Friday, 24 November 2023 at 09:35:00 UTC, BoQsc wrote:
> I tried to look into https://dlang.org/phobos/std_conv.html
>
> Most of the functions inside `std.conv` seem to be dependant on 
> [Garbage Collection](https://dlang.org/spec/garbage.html).
>
> And I couldn't find a straightforward way to produce a `string` 
> value out of `uint` value.
>
> How to convert or parse `uint` value to a `string` in `@nogc` 
> way?

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);
}
```


More information about the Digitalmars-d-learn mailing list