C# Unicode 字符串输出 [英] C# Unicode string output
问题描述
我有一个将字符串转换为 Unicode 字符串的函数:
I have a function to convert a string to a Unicode string:
private string UnicodeString(string text)
{
return Encoding.UTF8.GetString(Encoding.ASCII.GetBytes(text));
}
但是当我调用这个函数时,输出结果是错误的.看起来我的功能不起作用.
But when I am calling this function the output result is wrong. It looks like my function is not working.
Console.WriteLine(UnicodeString("добры дзень"))
在控制台上打印这样的问题:?????????
Console.WriteLine(UnicodeString("добры дзень"))
printing on console just questions like that: ????? ????
有没有办法让控制台正确显示?
Is there a way to say to console to display it correct?
更新
看起来问题不在 Unicode 中.我想它可能显示问号是因为我在系统中没有正确的区域设置(Windows 7)?
It looks like the problem not in Unicode. I think maybe it is displaying question marks because I am not having the correct locale in the system (Windows 7)?
有没有办法让它在不改变语言环境的情况下工作?
Is there a way to make it work without changing locale?
推荐答案
首先将输出编码改为UTF8:
First, change the output encoding to UTF8:
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("добры дзень");
现在您仍会看到问号.原因是默认控制台的字体不支持西里尔字母.更改控制台的字体:
Now you'll still see question marks. The reason is that the default console's font doesn't support Cyrillic letters. Change the font of the console:
如果幸运的话,您应该会找到一种支持 Unicode 的不同字体:
If you're lucky, you should find a different font with Unicode support:
更改字体,您应该可以看到您的文字:
Change the font, and you should be able to see your text:
在一般情况下,如果您想可靠地显示所有 Unicode 字符,控制台可能不适合您.另请参阅:C# 控制台字体(评论也很有趣)
In the general case, if you want to display all Unicode characters reliably, the Console is probably not right for you. See also: C# console font (the comments are interesting too)
这篇关于C# Unicode 字符串输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!