www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to write asia characters on console?

reply "Lave Zhang" <61843940 qq.com> writes:
Hi,

My first D program is like this:
-----------------------------------
import std.stdio;

void main(string[] args)
{
       dstring s1 = "hello你好"d;
       writeln(s1);
}
-----------------------------------
But the output is not correct(and my console codepage is 936):

C:\D\dmd2\samples\d>dmd hello.d -offilename hello.exe
C:\D\dmd2\samples\d>hello.exe
hello浣犲ソ

thanks.
Feb 07 2015
next sibling parent "weaselcat" <weaselcat gmail.com> writes:
On Sunday, 8 February 2015 at 05:57:31 UTC, Lave Zhang wrote:
 Hi,

 My first D program is like this:
 -----------------------------------
 import std.stdio;

 void main(string[] args)
 {
       dstring s1 = "hello你好"d;
       writeln(s1);
 }
 -----------------------------------
 But the output is not correct(and my console codepage is 936):

 C:\D\dmd2\samples\d>dmd hello.d -offilename hello.exe
 C:\D\dmd2\samples\d>hello.exe
 hello浣犲ソ

 thanks.
Hi, I tried your code and it works fine for me. I think the windows console only supports UTF-16, try using wchar/wstring instead of dchar/dstring.
Feb 07 2015
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/07/2015 09:57 PM, Lave Zhang wrote:
 Hi,

 My first D program is like this:
 -----------------------------------
 import std.stdio;

 void main(string[] args)
 {
        dstring s1 = "hello你好"d;
        writeln(s1);
 }
 -----------------------------------
 But the output is not correct(and my console codepage is 936):

 C:\D\dmd2\samples\d>dmd hello.d -offilename hello.exe
 C:\D\dmd2\samples\d>hello.exe
 hello浣犲ソ

 thanks.
This thread may be useful: http://forum.dlang.org/thread/suzymdzjeifnfirtbnrc dfeed.kimsufi.thecybershadow.net#post-suzymdzjeifnfirtbnrc:40dfeed.kimsufi.thecybershadow.net Ali
Feb 07 2015
parent reply "Lave Zhang" <61843940 qq.com> writes:
On Sunday, 8 February 2015 at 06:26:38 UTC, Ali Çehreli wrote:
 On 02/07/2015 09:57 PM, Lave Zhang wrote:
 Hi,

 My first D program is like this:
 -----------------------------------
 import std.stdio;

 void main(string[] args)
 {
       dstring s1 = "hello你好"d;
       writeln(s1);
 }
 -----------------------------------
 But the output is not correct(and my console codepage is 936):

 C:\D\dmd2\samples\d>dmd hello.d -offilename hello.exe
 C:\D\dmd2\samples\d>hello.exe
 hello浣犲ソ

 thanks.
This thread may be useful: http://forum.dlang.org/thread/suzymdzjeifnfirtbnrc dfeed.kimsufi.thecybershadow.net#post-suzymdzjeifnfirtbnrc:40dfeed.kimsufi.thecybershadow.net Ali
Thanks, my problem has been solved:) --------------------------------------- import std.stdio; import core.stdc.wchar_; extern(C) int setlocale(int, char*); static this() { fwide(core.stdc.stdio.stdout, 1); setlocale(0, cast(char*)"china"); } int main(string[] args) { string s1 = "hello你好"; writefln("%s", s1); return 0; }
Feb 08 2015
parent "mzfhhhh" <mzfhhhh foxmail.com> writes:
 Thanks, my problem has been solved:)
 ---------------------------------------
 import std.stdio;
 import core.stdc.wchar_;

 extern(C) int setlocale(int, char*);

 static this()
 {
    fwide(core.stdc.stdio.stdout, 1);
    setlocale(0, cast(char*)"china");
 }

 int main(string[] args)
 {
 	string s1 = "hello你好";
 	writefln("%s", s1);
 	return 0;
 }
This solution has a problem, look at this: http://forum.dlang.org/thread/jfawjvoedsxsznsuejoh forum.dlang.org#post-tyslcbycorgfaqimldnd:40forum.dlang.org
Feb 08 2015
prev sibling parent "FrankLike" <1150015857 qq.com> writes:
On Sunday, 8 February 2015 at 05:57:31 UTC, Lave Zhang wrote:
 Hi,

 My first D program is like this:
 -----------------------------------
 import std.stdio;

 void main(string[] args)
 {
       dstring s1 = "hello你好"d;
       writeln(s1);
 }
 -----------------------------------
 But the output is not correct(and my console codepage is 936):

 C:\D\dmd2\samples\d>dmd hello.d -offilename hello.exe
 C:\D\dmd2\samples\d>hello.exe
 hello浣犲ソ

 thanks.
add such code is better than use 'chcp 65001' for exe's consumer. extern(C) int setlocale(int, char*); static this() { import core.stdc.wchar_; fwide(core.stdc.stdio.stdout, 1); setlocale(0, cast(char*)"china"); } ----------------------------------------------------------------------- module cnsetlocale; import std.stdio; extern(C) int setlocale(int, char*); static this() { import core.stdc.wchar_; fwide(core.stdc.stdio.stdout, 1); setlocale(0, cast(char*)"china"); } void main() { printf("a!!\n"); writefln("%s","hello,中文!!"); string msg="你好!!"; string msg1="去看电影吗?"; string msg2="是吗?"; a(msg2); writefln("the string is %s \n %s %s and %s",msg,msg1,msg2,"你要去哪儿玩?"); writeln("公园?"," 电影院?"); readln; } void a(string msg2) { writefln("the string is %s",msg2); } ------------ dmd hello //only such simple 'printf' is error,but for consumer is better,because they don't need change any things,so don't use the 'printf' function . If only test for yourself,you can add such codes for your main : import std.process; executeShell("chcp 65001"); then,change the Font to 'Lucida Console' after 'dmd hello' ( if you don't want use 65001,then open cmd,chcp 936 ,and change the Font to '宋体') ok.
Feb 12 2015