print ubyte[] as (ascii) string

eugene dee0xeed at gmail.com
Thu Dec 30 17:31:27 UTC 2021


On Thursday, 30 December 2021 at 16:49:17 UTC, Tejas wrote:
> I _think_ the above code is correct, please verify


Self-contained example:

```d
import std.stdio;
import std.string;

void main() {
     ubyte[8] b = [0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0A, 0x00, 0x00];
     /* "hello\n\0\0" */

     char[] s = fromStringz(cast(char*)b.ptr);
     writefln("'%s, world'", s.strip);

     s = cast(char[])b[0 .. $];
     writefln("'%s, world'", s.strip);
}
```

Output:

```
@mono:~/2-coding/d-lang/misc$ ./p
'hello, world'
'hello
, world'
```



More information about the Digitalmars-d-learn mailing list