2.067 should modify writeln function, make it display correctly ANSI characters in CMD. Exe

FrankLike via Digitalmars-d digitalmars-d at puremagic.com
Sun Jan 25 08:09:58 PST 2015


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?


More information about the Digitalmars-d mailing list