String created from buffer has wrong length and strip() result is incorrect

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 17 08:30:43 PDT 2014


On Fri, 17 Oct 2014 15:24:21 +0000
Lucas Burson via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:

> So given the below buffer would I use fromStringz (is this in the 
> stdlib?) to cast it from a null-terminated buffer to a good 
> string? Shouldn't the compiler give a warning about casting a 
> buffer to a string without using fromStringz?
if you are really-really sure that your buffer is null-terminated, you
can use this trick:

  import std.conv;
  string s = to!string(cast(char*)buff.ptr);

please note, that this is NOT SAFE. you'd better doublecheck that your
buffer is not empty and is null-terminated.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141017/91c9aa87/attachment.sig>


More information about the Digitalmars-d-learn mailing list