String created from buffer has wrong length and strip() result is incorrect
Lucas Burson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 17 09:08:04 PDT 2014
On Friday, 17 October 2014 at 15:30:52 UTC, ketmar via
Digitalmars-d-learn wrote:
> 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.
The buffer is populated from a scsi ioctl so it "should" be only
ascii and null-terminated but it's a good idea to harden the code
a bit.
Thank you for your help!
More information about the Digitalmars-d-learn
mailing list