On Wednesday, 14 November 2012 at 13:37:26 UTC, Knud Soerensen wrote:
Hi

I am working with a c library which return a unsigned char *

As suggested on http://digitalmars.com/d/1.0/htomodule.html

I have converted it to (ubyte *).

Now is there an easy way to convert this to a string ?

import std.conv;
void main() {
    char[] s = "aoeu\0".dup;
    ubyte* bs = cast(ubyte*)s.ptr;

    // If you know the length, you can use a slice
    char[] s2 = cast(char[])bs[0..s.length];
    // this includes \0
    assert(s == s2);
    // but you could of course have used s.length-1

    // or you can walk and look for \0
    char[] s3 = to!(char[])(s.ptr);
    // note that to doesn't keep the zero terminator
    assert(s3 == s[0..$-1]);
}

Reply via email to