Jump to page: 1 2
Thread overview
Something about Chinese Disorder Code
Nov 24, 2015
magicdmer
Nov 24, 2015
Rikki Cattermole
Nov 24, 2015
Meta
Nov 25, 2015
Rikki Cattermole
Dec 02, 2015
Daniel Murphy
Nov 24, 2015
Andrea Fontana
Nov 24, 2015
BLM768
Nov 24, 2015
Marco Leise
Nov 25, 2015
magicdmer
Nov 25, 2015
Kagamin
Nov 25, 2015
Marc Schütz
Nov 26, 2015
magicdmer
Nov 26, 2015
Marc Schütz
Nov 27, 2015
magicdmer
November 24, 2015
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
November 24, 2015
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.
November 24, 2015
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.
November 24, 2015
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;
November 24, 2015
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.
November 24, 2015
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 = "你好,世界"
> > 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.

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 łáö 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.

-- 
Marco

November 25, 2015
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.
November 25, 2015
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);
November 25, 2015
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?
November 25, 2015
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);


« First   ‹ Prev
1 2