www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Something about Chinese Disorder Code

reply magicdmer <magicdmer gmail.com> writes:
I display chinese string like:

auto str = "你好,世界"
writeln(str)

and The display is garbled。

some windows api like MessageBoxA ,if string is chinese, it 
displays disorder code too

i think i must use WideCharToMultiByte to convert it , is there 
any other answer to solve this question simplely
Nov 24 2015
next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 24/11/15 10:48 PM, magicdmer wrote:
 I display chinese string like:

 auto str = "你好,世界"
 writeln(str)

 and The display is garbled。

 some windows api like MessageBoxA ,if string is chinese, it displays
 disorder code too

 i think i must use WideCharToMultiByte to convert it , is there any
 other answer to solve this question simplely
*A windows API functions are for ASCII only. You need the *W varients which are unicode. For this you could use std.conv : to. wstring text = "my str".to!wstring; Or at least I'm pretty sure that will work for you.
Nov 24 2015
parent reply Meta <jared771 gmail.com> writes:
On Tuesday, 24 November 2015 at 09:52:21 UTC, Rikki Cattermole 
wrote:
 On 24/11/15 10:48 PM, magicdmer wrote:
 I display chinese string like:

 auto str = "你好,世界"
 writeln(str)

 and The display is garbled。

 some windows api like MessageBoxA ,if string is chinese, it 
 displays
 disorder code too

 i think i must use WideCharToMultiByte to convert it , is 
 there any
 other answer to solve this question simplely
*A windows API functions are for ASCII only. You need the *W varients which are unicode. For this you could use std.conv : to. wstring text = "my str".to!wstring; Or at least I'm pretty sure that will work for you.
I'm pretty sure you can just do: wstring text = "my string"; Or auto text = "my string"w;
Nov 24 2015
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 25/11/15 1:47 AM, Meta wrote:
 On Tuesday, 24 November 2015 at 09:52:21 UTC, Rikki Cattermole wrote:
 On 24/11/15 10:48 PM, magicdmer wrote:
 I display chinese string like:

 auto str = "你好,世界"
 writeln(str)

 and The display is garbled。

 some windows api like MessageBoxA ,if string is chinese, it displays
 disorder code too

 i think i must use WideCharToMultiByte to convert it , is there any
 other answer to solve this question simplely
*A windows API functions are for ASCII only. You need the *W varients which are unicode. For this you could use std.conv : to. wstring text = "my str".to!wstring; Or at least I'm pretty sure that will work for you.
I'm pretty sure you can just do: wstring text = "my string"; Or auto text = "my string"w;
The second one is correct yes. I'm just assuming that it isn't compiled into the executable.
Nov 24 2015
parent Daniel Murphy <yebbliesnospam gmail.com> writes:
On 25/11/2015 2:16 PM, Rikki Cattermole wrote:
 On 25/11/15 1:47 AM, Meta wrote:
 I'm pretty sure you can just do:

 wstring text = "my string";

 Or

 auto text = "my string"w;
The second one is correct yes. I'm just assuming that it isn't compiled into the executable.
Either is fine. Non-suffixed string literals have a default type of string, but implicitly convert to wstring/dstring at compile time.
Dec 01 2015
prev sibling next sibling parent Andrea Fontana <nospam example.com> writes:
On Tuesday, 24 November 2015 at 09:48:45 UTC, magicdmer wrote:
 I display chinese string like:

 auto str = "你好,世界"
 writeln(str)

 and The display is garbled。

 some windows api like MessageBoxA ,if string is chinese, it 
 displays disorder code too

 i think i must use WideCharToMultiByte to convert it , is there 
 any other answer to solve this question simplely
MessageBoxA <- A is ANSI. You should try with MessageBoxW, for unicode I guess.
Nov 24 2015
prev sibling parent reply BLM768 <blm768 gmail.com> writes:
On Tuesday, 24 November 2015 at 09:48:45 UTC, magicdmer wrote:
 I display chinese string like:

 auto str = "你好,世界"
 writeln(str)

 and The display is garbled。

 some windows api like MessageBoxA ,if string is chinese, it 
 displays disorder code too

 i think i must use WideCharToMultiByte to convert it , is there 
 any other answer to solve this question simplely
You can also try using a wide string literal, i.e. "你好,世界"w. The suffix forces the string to use 16-bit characters. The garbled display from writeln might be related to your console settings. If you aren't using the UTF-8 codepage in cmd.exe, that would explain why the text appears garbled. Unfortunately, Windows has some significant bugs with UTF-8 in the console.
Nov 24 2015
parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Tue, 24 Nov 2015 17:08:33 +0000
schrieb BLM768 <blm768 gmail.com>:

 On Tuesday, 24 November 2015 at 09:48:45 UTC, magicdmer wrote:
 I display chinese string like:

 auto str =3D "=E4=BD=A0=E5=A5=BD=EF=BC=8C=E4=B8=96=E7=95=8C"
 writeln(str)

 and The display is garbled=E3=80=82

 some windows api like MessageBoxA ,if string is chinese, it=20
 displays disorder code too

 i think i must use WideCharToMultiByte to convert it , is there=20
 any other answer to solve this question simplely
=20 You can also try using a wide string literal, i.e. "=E4=BD=A0=E5=A5=BD=EF=
=BC=8C=E4=B8=96=E7=95=8C"w. The=20
 suffix forces the string to use 16-bit characters.
=20
 The garbled display from writeln might be related to your console=20
 settings. If you aren't using the UTF-8 codepage in cmd.exe, that=20
 would explain why the text appears garbled. Unfortunately,=20
 Windows has some significant bugs with UTF-8 in the console.
This is really our problem. We pretend the output terminal is UTF-8 without providing any means to configure the terminal or converting the output to the terminal's encoding. All OSs provide conversion API that works for the most part (assuming normalization form D for example), but we just don't use them. Most contributers were on Linux or English Windows system where the issue is not obvious. But even in Europe =C5=82=C3=A1=C3=B6 will come out garbled. Now this is mostly not an issue with modern Linux and OS X as the default is UTF-8, but some people refuse to change to variable length encodings and Windows has always been preferring fixed length encodings. The proper way to solve this for the OP in a cross-platform way is to replace writeln with something that detects whether the output is a terminal and then converts the string to something that will display correctly, which can be a simple wchar[] on Windows or the use of iconv on Linux. Ketmar is currently using iconv to print D string on his Linux set up for Cyrillic KIO-8U and I think I used it in some code as well. It is not perfect, but will make most printable strings readable. ICU I think is the only library that gets it 100% correct with regards to normalization and offering you different error concealment methods. --=20 Marco
Nov 24 2015
parent reply magicdmer <magicdmer gmail.com> writes:
On Tuesday, 24 November 2015 at 19:41:12 UTC, Marco Leise wrote:
 Am Tue, 24 Nov 2015 17:08:33 +0000
 schrieb BLM768 <blm768 gmail.com>:

 [...]
thank you for your answers. I solved it. windows console like: fwide(core.stdc.stdio.stdout, 1); setlocale(0, cast(char*)"china"); auto str = "你好,世界"; writeln(str); MessageBox like: const wchar *wstring = toUTF16z("你好,世界"); MessageBoxW(null,wstring,wstring,0);
Nov 24 2015
next sibling parent Kagamin <spam here.lot> writes:
On Wednesday, 25 November 2015 at 04:09:29 UTC, magicdmer wrote:
 fwide(core.stdc.stdio.stdout, 1);
 setlocale(0, cast(char*)"china");
 auto str = "你好,世界";
 writeln(str);
Is it for microsoft runtime or for snn?
Nov 25 2015
prev sibling parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Wednesday, 25 November 2015 at 04:09:29 UTC, magicdmer wrote:
 On Tuesday, 24 November 2015 at 19:41:12 UTC, Marco Leise wrote:
 Am Tue, 24 Nov 2015 17:08:33 +0000
 schrieb BLM768 <blm768 gmail.com>:

 [...]
thank you for your answers. I solved it. windows console like: fwide(core.stdc.stdio.stdout, 1); setlocale(0, cast(char*)"china");
You shouldn't need this cast. Try removing it; if it doesn't compile, something's wrong.
 auto str = "你好,世界";
 writeln(str);

 MessageBox like:
 const wchar *wstring = toUTF16z("你好,世界");
 MessageBoxW(null,wstring,wstring,0);
Nov 25 2015
parent reply magicdmer <magicdmer gmail.com> writes:
On Wednesday, 25 November 2015 at 12:33:00 UTC, Marc Schütz wrote:
 On Wednesday, 25 November 2015 at 04:09:29 UTC, magicdmer wrote:
 On Tuesday, 24 November 2015 at 19:41:12 UTC, Marco Leise 
 wrote:
 Am Tue, 24 Nov 2015 17:08:33 +0000
 schrieb BLM768 <blm768 gmail.com>:

 [...]
thank you for your answers. I solved it. windows console like: fwide(core.stdc.stdio.stdout, 1); setlocale(0, cast(char*)"china");
You shouldn't need this cast. Try removing it; if it doesn't compile, something's wrong.
Above code is for chinese displaying in windows console,the windows console is acsii,if i remove it,the console will display garbled,look like the following picture http://postimg.org/image/9kf5wdnx1/ so,i must add it
Nov 25 2015
parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Thursday, 26 November 2015 at 01:59:12 UTC, magicdmer wrote:
 On Wednesday, 25 November 2015 at 12:33:00 UTC, Marc Schütz 
 wrote:
 On Wednesday, 25 November 2015 at 04:09:29 UTC, magicdmer 
 wrote:
 On Tuesday, 24 November 2015 at 19:41:12 UTC, Marco Leise 
 wrote:
 Am Tue, 24 Nov 2015 17:08:33 +0000
 schrieb BLM768 <blm768 gmail.com>:

 [...]
thank you for your answers. I solved it. windows console like: fwide(core.stdc.stdio.stdout, 1); setlocale(0, cast(char*)"china");
You shouldn't need this cast. Try removing it; if it doesn't compile, something's wrong.
Above code is for chinese displaying in windows console,the windows console is acsii,if i remove it,the console will display garbled,look like the following picture http://postimg.org/image/9kf5wdnx1/ so,i must add it
No, I'm talking specifically about the cast in there, not the call to setlocale(). Does it still work if you replace this: setlocale(0, cast(char*)"china"); by that: setlocale(0, "china"); ?
Nov 26 2015
parent magicdmer <magicdmer gmail.com> writes:
On Thursday, 26 November 2015 at 09:59:01 UTC, Marc Schütz wrote:
 No, I'm talking specifically about the cast in there, not the 
 call to setlocale(). Does it still work if you replace this:

     setlocale(0, cast(char*)"china");

 by that:

     setlocale(0, "china");

 ?
yes Of course,it works well :) I copy the code from internet and not notice that,thank you
Nov 26 2015