Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Many people want to display their string(ANSI),but always to convert string by 'toMBSz',and it can't convert to immutable char[],it's difficult to display their ANSI string in CMD.EXE. We can use 'chcp 65001',but for exe'consumer,it's difficult to use. Now,I find a function,if add it in 'writeln' function,D will very be popular! string toMBS(string s, uint codePage = 0) { // Only need to do this if any chars have the high bit set foreach (char c; s) { if (c >= 0x80) { char[] result; int readLen; const wchar* ws = std.utf.toUTF16z(s); result.length = WideCharToMultiByte(codePage, 0, ws, -1, null, 0,null, null); if (result.length) { readLen = WideCharToMultiByte(codePage, 0, ws, -1, result.ptr,result.length, null, null); } if (!readLen || readLen != result.length) { throw new Exception("Couldn't convert string: " ~ sysErrorString(GetLastError())); } return cast(string)result[0..$-1]; } } return s; } Now ,I use it : string toStr(string s) { return toMBS(s,0); } writeln(toStr("中文")); So difficult! What do you think? |
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | On Sunday, 25 January 2015 at 16:09:59 UTC, FrankLike wrote: > Many people want to display their string(ANSI),but always to convert string by > > 'toMBSz',and it can't convert to immutable char[],it's difficult to display their ANSI > > string in CMD.EXE. > > > What do you think? But .. some people switch cmd.exe to utf8, so you'll break them. http://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8 not to mention all the linux people it'll break. Perhaps: import std.windows.charset; import std.conv; then: writeln(to!(string)(toMBSz(utf8))); |
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to AndyC | On Sunday, 25 January 2015 at 16:22:20 UTC, AndyC wrote: > On Sunday, 25 January 2015 at 16:09:59 UTC, FrankLike wrote: >> Many people want to display their string(ANSI),but always to convert string by >> >> 'toMBSz',and it can't convert to immutable char[],it's difficult to display their ANSI >> >> string in CMD.EXE. >> >> >> What do you think? > > But .. some people switch cmd.exe to utf8, so you'll break them. > > http://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8 > > not to mention all the linux people it'll break. > > Perhaps: > > import std.windows.charset; > import std.conv; > > then: > > writeln(to!(string)(toMBSz(utf8))); Why not switch by Version: version(Windows) { writeln(to!(string)(toMBSz(utf8))); } else version(linux) { ... } |
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | On Sunday, 25 January 2015 at 16:33:51 UTC, FrankLike wrote: >> Perhaps: >> >> import std.windows.charset; >> import std.conv; >> >> then: >> >> writeln(to!(string)(toMBSz(utf8))); > > Why not switch by Version: > version(Windows) > { > writeln(to!(string)(toMBSz(utf8))); > } > else version(linux) > { > ... > } writeln(to!(string)(toMBSz("中文1")," ",to!(string)(toMBSz(中文2)); Can't easy to do: writeln("中文1"," ","中文2"); |
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to AndyC | On Sunday, 25 January 2015 at 16:22:20 UTC, AndyC wrote:
> http://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8
We can use 'chcp 65001',but for exe'consumer,it's difficult to use.
Exe'Consumer! they are difficult to use!
|
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | Why we can not simply automatically switch CMD to UTF-8 before app start? I do not see any minuses in this solution. |
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike Attachments: | On Sun, 25 Jan 2015 16:55:29 +0000, FrankLike wrote: > On Sunday, 25 January 2015 at 16:22:20 UTC, AndyC wrote: > >> http://superuser.com/questions/269818/change-default-code-page-of- windows-console-to-utf-8 > > We can use 'chcp 65001',but for exe'consumer,it's difficult to use. > > Exe'Consumer! they are difficult to use! you can provide .bat file that will change codepage and run your exe. |
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | Windows console is broken, I recommend using API functions (WriteConsole) instead. |
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Krejcirik | On Sunday, 25 January 2015 at 16:09:59 UTC, FrankLike wrote: > What do you think? https://issues.dlang.org/show_bug.cgi?id=2742 On Sunday, 25 January 2015 at 18:47:31 UTC, Martin Krejcirik wrote: > Windows console is broken, I recommend using API functions (WriteConsole) instead. Well, that function writes to the windows console. |
January 25, 2015 Re: 2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Sunday, 25 January 2015 at 18:23:03 UTC, Suliman wrote:
> Why we can not simply automatically switch CMD to UTF-8 before app start?
> I do not see any minuses in this solution.
+1. I use
import std.stdio;
import std.c.windows.windows;
void main()
{
SetConsoleOutputCP(65001);
writeln(utf-8 text);
}
|
Copyright © 1999-2021 by the D Language Foundation