print ubyte[] as (ascii) string

Tejas notrealemail at gmail.com
Thu Dec 30 17:43:14 UTC 2021


On Thursday, 30 December 2021 at 17:31:27 UTC, eugene wrote:
> 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'
> ```

I'm not at my computer anymore, could you please replace the 
`0x00` with `0x0A` and tell me if `strip` still doesn't work for 
my solution?

I think the `fromstringz` is trimming the null bytes for you, 
making the `\n` the last character, allowing `strip` to work.



More information about the Digitalmars-d-learn mailing list