www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - converting c's c* to d strings

reply llee <llee goucher.edu> writes:
Does anyone know how to convert from character array pointers in c to d strings
using Phobos? 
Jun 28 2008
parent reply Xinok <xnknet gmail.com> writes:
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)];
Jun 28 2008
parent reply torhu <no spam.invalid> writes:
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.
Jun 28 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"torhu" <no spam.invalid> wrote in message 
news:g46fa8$1hli$1 digitalmars.com...
 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.

No is isn't, it does the exact same thing as what was posted above (cstr[0 .. strlen(cstr)]).
Jun 28 2008
parent reply torhu <no spam.invalid> writes:
Jarrett Billingsley wrote:
 "torhu" <no spam.invalid> wrote in message 
 news:g46fa8$1hli$1 digitalmars.com...
 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.

No is isn't, it does the exact same thing as what was posted above (cstr[0 ... strlen(cstr)]).

Aaaaaaaargh! It's toStringz that copies the string. Can't remember the last time I was wrong about anything. :( In my defence, phobos 2.0 toString(char*) does actually copy...
Jun 28 2008
parent novice2 <sorry noem.ail> writes:
i just want to remember about unicode:
when you pass D char[] to windows API *char
you can use std.windows.charset.toMBSz()

when you pass windows API *char to D char[]
you can use std.windows.charset.fromMBSz()
Jun 28 2008