converting c's c* to d strings

torhu no at spam.invalid
Sat Jun 28 15:52:07 PDT 2008


Xinok wrote:
> llee wrote:
>> Does anyone know how to convert from character array pointers in c to d strings using Phobos? 
> 
> You can convert any pointer type to an array type using slicing.
> 
> import std.c.string;
> char* cstr;
> string dstr = cstr[0..strlen(cstr)];

Or you can use std.string.toString().  It's cleaner, but slower because 
it copies the string.

To go the other way, use toStringz().  Or do dstr ~'\0'.

D string literals always have a null terminator appended so you can use 
them directly with C functions.



More information about the Digitalmars-d mailing list