print ubyte[] as (ascii) string

eugene dee0xeed at gmail.com
Sun Jan 2 08:48:27 UTC 2022


On Saturday, 1 January 2022 at 09:34:10 UTC, Jack Applegame wrote:
> On Thursday, 30 December 2021 at 18:07:15 UTC, eugene wrote:
>> On Thursday, 30 December 2021 at 17:52:20 UTC, eugene wrote:
>> much better than my initial
>
> You can also write
> ```d
> auto s = cast(string)b; // or cast(string)(b)
> ```
> instead of
> ```d
> char[] s = cast(char[])b[0 .. $];
> ```

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

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

     auto s = cast(string)b;
     writefln("'%s, world'", s);
     writefln("'%s, world'", s.strip("\n\x00"));
}

```

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

```



More information about the Digitalmars-d-learn mailing list