string to wchar*?

Stanislav Blinov via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 3 17:45:23 PDT 2017


On Saturday, 3 June 2017 at 23:36:18 UTC, Mike B Johnson wrote:

>> https://dlang.org/phobos/std_utf.html#toUTF16z
>
> This didn't work. More errors than the first.

Works for me:

void main()
{
     import std.conv;
     import std.stdio;
     import core.stdc.wchar_;
     import core.stdc.stdio;

     auto f = fopen("hello.bin", "w,ccs=UTF16LE");
     scope (exit) fclose(f);

     import std.utf;
     string hello = "Привет";
     wchar bom = '\ufeff';
     auto str = hello.toUTF16z;
     fputwc(bom, f);
     while (str && *str) {
         fputwc(*str, f);
         ++str;
     }
}

$ rdmd wchartest.d
$ file hello.bin
hello.bin: Little-endian UTF-16 Unicode text, with no line 
terminators

$ hexdump hello.bin
0000000 feff 041f 0440 0438 0432 0435 0442

$ iconv -f UTF-16LE hello.bin
Привет

> In any case, it conv should work.

No, it shouldn't. char* et al. are not string types in D. 
to!(char*)(string) just doesn't make sense.


More information about the Digitalmars-d-learn mailing list