print ubyte[] as (ascii) string

Tejas notrealemail at gmail.com
Thu Dec 30 17:16:10 UTC 2021


On Thursday, 30 December 2021 at 17:07:20 UTC, eugene wrote:
> On Thursday, 30 December 2021 at 16:49:17 UTC, Tejas wrote:
>> ```d
>> char[] s = cast(char[])ioCtx.buf[0 .. $];// please remember 
>> that in `[0 .. $]` last index is automatically `length - 1` 
>> but just buf[$] will be an error since there the actual 
>> `length` will be used
>> ```
>>
>> I _think_ the above code is correct, please verify
>
> There is one pecularity:
>
> ```d
> char[] s = fromStringz(cast(char*)ioCtx.buf.ptr);
> writefln("got '%s' from '%s:%d'", s.strip, client.addr, 
> client.port);
> // strip works :)
> ```
>
> ```d
> char[] s = cast(char[])ioCtx.buf[0 .. $];
> writefln("got '%s' from '%s:%d'", s.strip, client.addr, 
> client.port);
> // strip does not work :(
> ```

I'll need to know the error message, because the following works:
```d
import std;
void main()
{
     ubyte[] c ;
     c.length = 100;
     char[] arr = cast(char[])c[0 .. $];
     foreach(ref elem; arr)
         elem = 'a';
     writeln(arr.strip);
}
```


More information about the Digitalmars-d-learn mailing list