wchar* to char[]

doob doobnet at gmail.com
Thu Feb 7 05:25:07 PST 2008


Sergey Gromov wrote:
> Heinz <malagana15 at yahoo.es> wrote:
>> Is there a way to convert a wchar* variable to char[]? I'm trying to use the Win32 API LoadStringW, wich takes a parameter a wchar*.
> 
> Assuming that `src' is a wchar* string, in Phobos it's like this:
> 
>   import std.utf;
>   extern(C) uint wcslen(in wchar* str);	// a C runtime library function
> 
>   char[] str = toUTF8(src[0..wcslen(src)]);
> 
> In Tango, this would be:
> 
>   import tango.text.convert.Utf;
>   import tango.text.Util;
> 
>   auto len = indexOf(src, '\u0000', uint.max);
>   char[] str = toString(src[0..len]);
> 

I would do like this:

Phobos:

import std.string;
import std.utf;

// alias for char[] (D 1.x)
string str = toUTF8(toString(src));

Tango:

import tango.stdc.stringz;
import tango.text.convert.Utf;

char[] str = toString(fromString16z(str));


More information about the Digitalmars-d-learn mailing list