string to wchar*?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 3 16:51:35 PDT 2017


On 06/03/2017 04:36 PM, Mike B Johnson wrote:
> On Saturday, 3 June 2017 at 23:09:56 UTC, Stanislav Blinov wrote:
>> On Saturday, 3 June 2017 at 22:54:22 UTC, Mike B Johnson wrote:
>>
>>> How to convert a string to wchar*?
>>
>> C-style null-terminated wchar*?
>>
>> https://dlang.org/phobos/std_utf.html#toUTF16z
>
> This didn't work. More errors than the first. In any case, it conv
> should work.

Worked for me:

import std.stdio;
import std.utf;

void main() {
     string s = "hello";
     s ~= " world";

     auto w = s.toUTF16z;

     // Rough length estimate (assuming that all characters in this
     // UFT-16 encoded string are 2-byte long)
     // And +1 is for the "null char"
     auto bytes = (cast(ubyte*)w)[0 .. s.length * 2 + 1];

     writefln("%(%02x %)", bytes);
}

Output:

68 00 65 00 6c 00 6c 00 6f 00 20 00 77 00 6f 00 72 00 6c 00 64 00 00

Ali



More information about the Digitalmars-d-learn mailing list