digitalmars.D.learn - string to wchar*?
- Mike B Johnson (22/22) Jun 03 2017 How to convert a string to wchar*?
- Stanislav Blinov (3/4) Jun 03 2017 C-style null-terminated wchar*?
- Mike B Johnson (3/7) Jun 03 2017 This didn't work. More errors than the first. In any case, it
- =?UTF-8?Q?Ali_=c3=87ehreli?= (17/27) Jun 03 2017 Worked for me:
- Stanislav Blinov (30/33) Jun 03 2017 Works for me:
- H. S. Teoh via Digitalmars-d-learn (9/11) Jun 03 2017 If you need to convert between D strings and char*, wchar*, etc., e.g.,
How to convert a string to wchar*? string s; to!(wchar*)(s) gives phobo's deduction problems. \dmd2\windows\bin\..\..\src\phobos\std\conv.d(194): Error: template std.conv.toImpl cannot deduce function from argument types !(wchar*)(string), candidates are: \dmd2\windows\bin\..\..\src\phobos\std\conv.d(435): std.conv.toImpl(T, S)(S value) if (isImplicitlyConvertible!(S, T) && !isEnumStrToStr!(S, T) && !isNullToStr!(S, T)) \dmd2\windows\bin\..\..\src\phobos\std\conv.d(549): std.conv.toImpl(T, S)(ref S s) if (isStaticArray!S) \dmd2\windows\bin\..\..\src\phobos\std\conv.d(565): std.conv.toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && is(typeof(S.init.opCast!T()) : T) && !isExactSomeString!T && !is(typeof(T(value)))) \dmd2\windows\bin\..\..\src\phobos\std\conv.d(616): std.conv.toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && is(T == struct) && is(typeof(T(value)))) \dmd2\windows\bin\..\..\src\phobos\std\conv.d(665): std.conv.toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && is(T == class) && is(typeof(new T(value))))
Jun 03 2017
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
Jun 03 2017
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:This didn't work. More errors than the first. In any case, it conv should work.How to convert a string to wchar*?C-style null-terminated wchar*? https://dlang.org/phobos/std_utf.html#toUTF16z
Jun 03 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: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 AliOn Saturday, 3 June 2017 at 22:54:22 UTC, Mike B Johnson wrote:This didn't work. More errors than the first. In any case, it conv should work.How to convert a string to wchar*?C-style null-terminated wchar*? https://dlang.org/phobos/std_utf.html#toUTF16z
Jun 03 2017
On Saturday, 3 June 2017 at 23:36:18 UTC, Mike B Johnson wrote: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 Приветhttps://dlang.org/phobos/std_utf.html#toUTF16zThis didn't work. More errors than the first.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.
Jun 03 2017
On Sun, Jun 04, 2017 at 12:45:23AM +0000, Stanislav Blinov via Digitalmars-d-learn wrote: [...]No, it shouldn't. char* et al. are not string types in D. to!(char*)(string) just doesn't make sense.If you need to convert between D strings and char*, wchar*, etc., e.g., for interfacing with C/C++ APIs, take a look at std.string.toStringz and std.string.fromStringz. Do not use casts or std.conv.to because D does not treat character pointers as string, unlike C/C++. T -- Leather is waterproof. Ever see a cow with an umbrella?
Jun 03 2017