How to write asia characters on console?

FrankLike via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 12 05:29:58 PST 2015


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.



More information about the Digitalmars-d-learn mailing list