Thread overview
wchar* to char[]
Feb 07, 2008
Heinz
Feb 07, 2008
Sergey Gromov
Feb 07, 2008
doob
Feb 07, 2008
Sergey Gromov
Feb 08, 2008
doob
February 07, 2008
Hi,

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*.

Thanx.
February 07, 2008
"Heinz" <malagana15@yahoo.es> wrote in message news:foe0pj$mct$1@digitalmars.com...
> Hi,
>
> 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*.
>

So, you have a function that takes a wchar*, and you have a wchar*.

..

I'm not seeing how char[] comes into this at all.


February 07, 2008
Heinz <malagana15@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]);

-- 
SnakE
February 07, 2008
Sergey Gromov wrote:
> Heinz <malagana15@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));
February 07, 2008
doob <doobnet@gmail.com> wrote:
> Sergey Gromov wrote:
> > Heinz <malagana15@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)]);
> 
> I would do like this:
> 
> Phobos:
> 
> import std.string;
> import std.utf;
> 
> // alias for char[] (D 1.x)
> string str = toUTF8(toString(src));

In Phobos, there is no function "std.string.toString(wchar*)".

-- 
SnakE
February 08, 2008
Sergey Gromov wrote:
> doob <doobnet@gmail.com> wrote:
>> Sergey Gromov wrote:
>>> Heinz <malagana15@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)]);
>> I would do like this:
>>
>> Phobos:
>>
>> import std.string;
>> import std.utf;
>>
>> // alias for char[] (D 1.x)
>> string str = toUTF8(toString(src));
> 
> In Phobos, there is no function "std.string.toString(wchar*)".
> 
 My bad, it was only for char*