digitalmars.D.learn - D2 string conversion
- Sam Hu (13/13) May 09 2009 Hello,
- Jarrett Billingsley (2/14) May 09 2009 Why do people always post problems and fail to post the errors they get?
- Sam Hu (65/65) May 09 2009 Sorry.
- John C (8/26) May 09 2009 The problem is that you're calling the UTF-16 versions of the function w...
- Sivo Schilling (22/40) May 09 2009 Hi Sam,
Hello, In D2 I tried to write a simple function which wrapps WIN32 API MessageBoxW,with which I can handle wide characters like Chinese .Below is my try: import std.string; import std.conv; import std.c.windows.windows; extern(Windows)int MessageBoxW(HWND,LPCSTR,LPCSTR,int); int showMessage(wchar[] msg,wchar[ ] caption=cast(wchar[])"Application",int style=MB_OK|MB_ICONINFORMATION) { return MessageBoxW(null,to!(const(char)[])(msg),to!(const(char)[])(c ption),style);//try one return MessageBoxW(null,toStringz(to!(const(char[])(msg)),toStringz(to!(const(char)[])(ca tion)),style);//try two } But both *try one* and *try two* the above are wrong.Anyone can figure me out what's the problem and what's the right way to do would be appreciated. Sam
May 09 2009
On Sat, May 9, 2009 at 11:01 AM, Sam Hu <samhudotsamhu gmail.com> wrote:Hello, In D2 I tried to write a simple function which wrapps WIN32 API MessageBoxW,with which I can handle wide characters like Chinese .Below is my try: import std.string; import std.conv; import std.c.windows.windows; extern(Windows)int MessageBoxW(HWND,LPCSTR,LPCSTR,int); int showMessage(wchar[] msg,wchar[ ] caption=cast(wchar[])"Application",int style=MB_OK|MB_ICONINFORMATION) { return MessageBoxW(null,to!(const(char)[])(msg),to!(const(char)[])(c ption),style);//try one return MessageBoxW(null,toStringz(to!(const(char[])(msg)),toStringz(to!(const(char)[])(ca tion)),style);//try two } But both *try one* and *try two* the above are wrong.Anyone can figure me out what's the problem and what's the right way to do would be appreciated.Why do people always post problems and fail to post the errors they get?
May 09 2009
Sorry. //Source: module d2001; import std.stdio; import std.c.stdlib; import std.c.windows.windows; import std.string; import std.conv; void pause() { writefln("Press any key to continue..."); getchar; } extern(Windows) int MessageBoxW(HWND,LPCSTR,LPCSTR,UINT); int showMessage(wchar[] msg,wchar[] caption=cast(wchar[])"Application", int style=MB_OK|MB_ICONINFORMATION) { return MessageBoxW(null,to!(const(char)[])(msg),to!(const(char)[])(c ption),style);//try one //try two: //return MessageBoxW(null,toStringz(to!(const(char[])(msg)),toStringz(to!(const(char)[])(caption)),style); } int main(string[] args) { writefln("Hello d2..."); showMessage("ÄãºÃ£¡"); pause; return 0; } For *try one* the error message is as below: G:\DLang\DEx\D2Ex>dmd d2001.d d2001.d(21): Error: function d2001.MessageBoxW (HANDLE, const(char)*, const(char )*, uint) does not match parameter types (void*,const(char)[],const(char)[],int) d2001.d(21): Error: cannot implicitly convert expression (to(msg)) of type const (char)[] to const(char)* d2001.d(21): Error: cannot implicitly convert expression (to(caption)) of type c onst(char)[] to const(char)* G:\DLang\DEx\D2Ex> For *try two* the erro message is as below: G:\DLang\DEx\D2Ex>dmd d2001.d d2001.d(23): expression expected, not 'const' d2001.d(23): found '[' when expecting '.' following 'char' d2001.d(23): found ']' when expecting identifier following 'char.' d2001.d(23): found ';' when expecting ',' d2001.d(25): expression expected, not '}' d2001.d(28): found 'int' when expecting ',' d2001.d(28): found 'args' when expecting ',' d2001.d(28): expression expected, not ')' d2001.d(29): found '{' when expecting ',' d2001.d(31): found ';' when expecting ',' d2001.d(32): found ';' when expecting ',' d2001.d(34): found ';' when expecting ',' d2001.d(36): expression expected, not 'return' d2001.d(36): found '0' when expecting ',' d2001.d(36): expression expected, not ';' d2001.d(38): found '}' when expecting ',' d2001.d(38): expression expected, not 'EOF' d2001.d(38): found 'EOF' when expecting ',' d2001.d(38): expression expected, not 'EOF' d2001.d(38): found 'EOF' when expecting ',' d2001.d(38): expression expected, not 'EOF' G:\DLang\DEx\D2Ex> DMD202+Phobos under xp sp3. Thanks for helping. Sam
May 09 2009
Sam Hu Wrote:Hello, In D2 I tried to write a simple function which wrapps WIN32 API MessageBoxW,with which I can handle wide characters like Chinese .Below is my try: import std.string; import std.conv; import std.c.windows.windows; extern(Windows)int MessageBoxW(HWND,LPCSTR,LPCSTR,int); int showMessage(wchar[] msg,wchar[ ] caption=cast(wchar[])"Application",int style=MB_OK|MB_ICONINFORMATION) { return MessageBoxW(null,to!(const(char)[])(msg),to!(const(char)[])(c ption),style);//try one return MessageBoxW(null,toStringz(to!(const(char[])(msg)),toStringz(to!(const(char)[])(ca tion)),style);//try two } But both *try one* and *try two* the above are wrong.Anyone can figure me out what's the problem and what's the right way to do would be appreciated. SamThe problem is that you're calling the UTF-16 versions of the function with UTF-8 strings. extern(Windows) int MessageBoxW(HWND, in wchar*, in wchar*, int); int showMessage(wstring msg, wstring caption = "Application", int style = MB_OK | MB_ICONINFORMATION) { auto pszMsg = (msg ~ "\000").ptr; auto pszCaption = (caption ~ "\000").ptr; return MessageBoxW(HWND.init, pszMsg, pszCaption, style); }
May 09 2009
Sam Hu Wrote:Hello, In D2 I tried to write a simple function which wrapps WIN32 API MessageBoxW,with which I can handle wide characters like Chinese .Below is my try: import std.string; import std.conv; import std.c.windows.windows; extern(Windows)int MessageBoxW(HWND,LPCSTR,LPCSTR,int); int showMessage(wchar[] msg,wchar[ ] caption=cast(wchar[])"Application",int style=MB_OK|MB_ICONINFORMATION) { return MessageBoxW(null,to!(const(char)[])(msg),to!(const(char)[])(c ption),style);//try one return MessageBoxW(null,toStringz(to!(const(char[])(msg)),toStringz(to!(const(char)[])(ca tion)),style);//try two } But both *try one* and *try two* the above are wrong.Anyone can figure me out what's the problem and what's the right way to do would be appreciated. SamHi Sam, here your code snippet with a few modifications to work: --- module sam; import std.string; import std.conv; import std.c.windows.windows; extern(Windows)int MessageBoxW(HWND, LPCWSTR, LPCWSTR, int); int showMessage(in wchar[] msg, in wchar[] caption = cast(wchar[])"Application", int style = MB_OK|MB_ICONINFORMATION) { // return MessageBoxW(null,to!(const(char)[])(msg),to!(const(char)[])(c ption),style);//try one return MessageBoxW(null, (msg ~ "\0").ptr, (caption ~ "\0").ptr, style); //try two } void main() { showMessage("Message with embedded \u00E4\u00E5\u00F6 european unicode chars"w); } ---- Hope this will help you. Chears, Sivo
May 09 2009
Hi Sivo & John, Thank you very much!!!The two versions all work. But I am a bit confused with the key word *in* in both versions.without them the compiler complained that: Sivo's version without keyword *in*: G:\DLang\DEx\D2Ex>dmd d2001.d d2001.d(32): Error: function d2001.showMessage (wchar[] msg, wchar[] caption = " Application", int style = 0 | 64) does not match parameter types (immutable(char )[]) d2001.d(32): Error: cannot implicitly convert expression ("\xe6\x88\x91\xe7\x9a\ x84\xe6\x9c\xaa\xe6\x9d\xa5\xe4\xb8\x8d\xe6\x98\xaf\xe6\xa2\xa6") of type immuta ble(char)[] to wchar[] G:\DLang\DEx\D2Ex> While John's version without keyword *in*: G:\DLang\DEx\D2Ex>dmd d2001.d d2001.d(27): Error: function d2001.MessageBoxW (HANDLE, wchar*, wchar*, int) doe s not match parameter types (HANDLE,immutable(wchar)*,immutable(wchar)*,int) d2001.d(27): Error: cannot implicitly convert expression (cast(immutable(wchar)* )(msg ~ "\x00")) of type immutable(wchar)* to wchar* d2001.d(27): Error: cannot implicitly convert expression (cast(immutable(wchar)* )(caption ~ "\x00")) of type immutable(wchar)* to wchar* G:\DLang\DEx\D2Ex> I am wondering why *in* should have to be used here. Sam
May 09 2009
Sam Hu Wrote:Hi Sivo & John, Thank you very much!!!The two versions all work. But I am a bit confused with the key word *in* in both versions.It's simple, really. Strings are immutable in D2. And "in" is equivalent to "immutable". By the way, best to use wstring (for UTF-16) or string (for UTF-8) in your parameter declarations.
May 10 2009