Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 03, 2017 string to wchar*? | ||||
---|---|---|---|---|
| ||||
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)))) |
June 03, 2017 Re: string to wchar*? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike B Johnson | 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 |
June 03, 2017 Re: string to wchar*? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | 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.
|
June 03, 2017 Re: string to wchar*? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike B Johnson | 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
|
June 04, 2017 Re: string to wchar*? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike B Johnson | 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. |
June 03, 2017 Re: string to wchar*? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | 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? |
Copyright © 1999-2021 by the D Language Foundation