www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to print Chinese characters in console in window XP?

reply Sam Hu <samhudotsamhu gmail.com> writes:
In DMD2030 I just found that Chinese characters the like can not print properly
into console.

//Below code does not show output properly in windows console:
void main()
{
wstring country="中国";
writefln(country);
}



Is this an OS issue or is there any way to reach the goal?

Thanks in advance.
Jun 27 2009
next sibling parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sat, Jun 27, 2009 at 5:53 AM, Sam Hu<samhudotsamhu gmail.com> wrote:
 In DMD2030 I just found that Chinese characters the like can not print pr=
operly into console.
 //Below code does not show output properly in windows console:
 void main()
 {
 wstring country=3D"=D6=D0=B9=FA";
 writefln(country);
 }



 Is this an OS issue or is there any way to reach the goal?
It's an OS issue. Someone asks about this once every two weeks. You have to set up cmd.exe to use Unicode. You have to change the fonts to Lucida, I think, and use 'chcp 65001' before running your program.
Jun 27 2009
parent reply Sam Hu <samhudotsamhu gmail.com> writes:
Jarrett Billingsley Wrote:

 On Sat, Jun 27, 2009 at 5:53 AM, Sam Hu<samhudotsamhu gmail.com> wrote:
 In DMD2030 I just found that Chinese characters the like can not print
properly into console.

 //Below code does not show output properly in windows console:
 void main()
 {
 wstring country="中国";
 writefln(country);
 }



 Is this an OS issue or is there any way to reach the goal?
It's an OS issue. Someone asks about this once every two weeks. You have to set up cmd.exe to use Unicode. You have to change the fonts to Lucida, I think, and use 'chcp 65001' before running your program.
Thanks so much for your help. Below is my try: 1.source(wrote in notepad.exe) module test; import std.stdio; import std.c.windows.windows; extern(Windows) {BOOL SetConsoleOutputCP(UINT wCodePageID);} void main() { SetConsoleOutputCP(65001); wstring country="中国"; writefln(country); } 2.set notepad font Lucida Console (acutally tried Lucida Sans Unicode also); 3.Save test.d in Unicode (actually also tried UTF-8); 4.Compile and run.But the output still doesn't make sense. Regards, Sam
Jun 27 2009
parent reply Sam Hu <samhudotsamhu gmail.com> writes:
and I start cmd.exe with /u switch.

I also tried being inspired by below article:
http://www.codeproject.com/KB/cpp/unicode_console_output.aspx?display=Print

but unfortunately also failed.
Jun 27 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sat, Jun 27, 2009 at 10:55 AM, Sam Hu<samhudotsamhu gmail.com> wrote:
 and I start cmd.exe with /u switch.

 I also tried being inspired by below article:
 http://www.codeproject.com/KB/cpp/unicode_console_output.aspx?display=Print

 but unfortunately also failed.
Well I don't know how else to help, I just know that cmd.exe sucks when it comes to Unicode :\
Jun 27 2009
parent reply Elrood <arno nym.net> writes:
Sorry to have to disappoint you, but Chinese output to a Windows console
is non-trivial if not entirely impossible (you'd probably have to ask
someone using a Chinese Windows version for a definitive answer).

Your approach will work for more common unicode characters, like eg. the
greek alphabet, the problem is that the fonts included with your
standard Windows just don't come with Chinese glyphs.

In theory you *could* hack the registry to enable different fonts for
the console (HKLM/SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Console\TrueTypeFont, add a string entry named 00,
000, 000 etc. and your desired font as value), but you'd still have to
find a font including Chinese glyphs which works for this purpose. There
are fonts like for example GNU unifont and something called SimSun from
Micro$oft which contain these glyphs, but to my knowledge none these
work in the console.

So the short answer is no, afaik there isn't any practical way to get
Chinese output in the Windows console.
Jun 27 2009
parent reply Rainer Deyke <rainerd eldwood.com> writes:
Elrood wrote:
 Sorry to have to disappoint you, but Chinese output to a Windows console
 is non-trivial if not entirely impossible (you'd probably have to ask
 someone using a Chinese Windows version for a definitive answer).
No, it's not. In Python, unicode text output just plain works. Program: # coding=utf-8 print u'涓浗' Output: 涓浗 This is a bug in D, full stop. (Yes, you do need to to set a codepage that contains these characters (I am using CP932) and you need an font that can represent these characters. I assume OP already has both. Note that CP65001 does not work.) -- Rainer Deyke - rainerd eldwood.com
Jun 27 2009
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Sat, 27 Jun 2009 16:13:03 -0600, Rainer Deyke wrote:

 Elrood wrote:
 Sorry to have to disappoint you, but Chinese output to a Windows console
 is non-trivial if not entirely impossible (you'd probably have to ask
 someone using a Chinese Windows version for a definitive answer).
No, it's not. In Python, unicode text output just plain works. Program: # coding=utf-8 print u'涓浗' Output: 涓浗 This is a bug in D, full stop. (Yes, you do need to to set a codepage that contains these characters (I am using CP932) and you need an font that can represent these characters. I assume OP already has both. Note that CP65001 does not work.)
I'm having troubles with this ... C:\temp>chcp 932 Invalid code page C:\temp>chcp 65001 Active code page: 65001 It seems that my Windows XP doesn't know about CP932. -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Jun 27 2009
parent Rainer Deyke <rainerd eldwood.com> writes:
Derek Parnell wrote:
 C:\temp>chcp 932
 Invalid code page
 C:\temp>chcp 65001
 Active code page: 65001
 
 It seems that my Windows XP doesn't know about CP932.
Yeah, it looks like some more setup work is required. I got the same error message when trying to use CP932 or CP950. Under Windows XP, in Regional and Language Options under the control panel: - On the Languages pane, make sure East Asian languages checked. - On the Advanced pane, make sure that the code pages you want to use are checked. - Also on the Advanced pane, you may need to select the language you want to use as the language for non-Unicode programs. However, I assume that the OP already has his computer set up to support Chinese characters from the console, or he wouldn't be asking this question. I can confirm that on my computer, unicode text output works in Python but not in D. -- Rainer Deyke - rainerd eldwood.com
Jun 27 2009
prev sibling parent reply Elrood <arno nym.net> writes:
 Rainer Deyke:
 In Python, unicode text output just plain works.
 
While it certainly wasn't my intent to spread false info, nor to defend D or sidetrack from its bugs, I cannot confirm that unicode output *just plain works*, neither with Python, and I'd be highly interested in more info on how you got it to work. CP932 is invalid on my machine as well, and I don't know of a font that supports Chinese symbols and is available for the Windows console. Could you post some further clues like which font and which version of Windows you use, please? That'd be highly appreciated, thanks in advance.
Jun 27 2009
next sibling parent Sam Hu <samhudotsamhu gmail.com> writes:
Dear All:

Thank you so much for all your help!

Yes,I am using xp+sp3 Chinese version and can print Chinese font in the console
in C++ program:

#include <iostream>
int main(int argc,char* argv[])
{
std::cout<<"中国"<<std::endl;
return 0;
}
Jun 27 2009
prev sibling parent reply Rainer Deyke <rainerd eldwood.com> writes:
Elrood wrote:
 While it certainly wasn't my intent to spread false info, nor to defend
 D or sidetrack from its bugs, I cannot confirm that unicode output *just
 plain works*, neither with Python, and I'd be highly interested in more
 info on how you got it to work.
There are two separate issues here. One is unicode output (which involves translating unicode text to the encoding used by the console). The other issue is having a console window that can display the characters that you want to display. The former can be tested without resorting to exotic code pages. Try to output '亩' in CP775 or '陌' in CP857. Both of these code pages are readily available. To enable Chinese characters, some more work is necessary. Under Windows XP, Control Panel, Regional and Language Options: - On the Languages pane, make sure East Asian Languages is checked. - On the Advanced pane, select a language that uses Chinese characters. - On the Advanced pane, make sure that the code pages you want to use are checked. -- Rainer Deyke - rainerd eldwood.com
Jun 28 2009
parent Elrood <arno nym.net> writes:
 Rainer Deyke:

Thanks for your clarification.
The problem is that, unless you set up your system to use Chinese for
every non-unicode-ready application, systemwide and for all users, you
will not get Chinese character output in the Windows console.

The downside is that setting up your OS in such a way tends to render
the user interface of a lot of other applications unusable if Chinese
isn't your everyday language.

For a lot of us this isn't very practicable, eg. having my German
umlauts replaced with Chinese glyphs isn't exactly what I have dreamed
of all my life.
That's what I meant with the changes not being trivial, and your
response sounded a bit like I was talking nonsense and getting it to
work was a piece of cake, and I naturally was curious whether there was
a simple solution. While being a bit disappointing, thanks for
straightening out that this isn't the case.

Nonetheless there apparently is a bug here if D/Phobos with its supposed
unicode support isn't capable of producing output which is possible with
Python or even olde C++.
Jun 29 2009
prev sibling parent Kagamin <spam here.lot> writes:
It's a known phobos bug
 http://d.puremagic.com/issues/show_bug.cgi?id=2742

You can use C api.
Jul 01 2009