如何从C#中的Windows控制台读取特殊字符(0x80 ... 0x9F)? [英] How do I read special characters (0x80 ... 0x9F) from the Windows console in C#?

查看:278
本文介绍了如何从C#中的Windows控制台读取特殊字符(0x80 ... 0x9F)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于解决了 David 的帮助下,向Windows控制台写入特殊字符(0x80 ... 0x9F) a>:


  1. 输出编码必须设置为UTF-8。

  2. 控制台使用的应该是Consolas。

现在我想能够读回包含特殊字符的文本在0x80-0x9F范围内(使用Windows 1252编码),例如EURO符号(€):

  string text = System.Console.ReadLine(); 

返回 null 特殊字符。我试着天真地设置 InputEncoding 为UTF8:

  System.Console .InputEncoding = System.Text.Encoding.UTF8; 

但这没有帮助。

解决方案

您可以将控制台应用程序的输入代码页设置为读取这些特殊字符。
有一个Win32 api名为SetConsoleCP来设置输入代码页。在下面的例子中我使用Windows-1252代码页:

  [DllImport(kernel32.dll)] 
private static extern bool SetConsoleCP(uint wCodePageID);

static void Main(string [] args)
{
SetConsoleCP((uint)1252);
Console.OutputEncoding = Encoding.UTF8;
System.Console.Out.WriteLine(œil);

string euro = Console.In.ReadLine();

Console.Out.WriteLine(euro);
}

EDIT: b

AS LB建议您还可以使用Console.InputEncoding = Encoding.GetEncoding(1252)。



下面是没有interop的完整示例(注意,您也可以使用Windows-输出编码:

  static void Main(string [] args)
{
Console.InputEncoding = Encoding.GetEncoding(1252);
Console.OutputEncoding = Encoding.GetEncoding(1252);
System.Console.Out.WriteLine(œil);

string euro = Console.In.ReadLine();

Console.Out.WriteLine(euro);
}

END EDIT



希望这有助于。


I've finally solved the issue of writing special characters (0x80...0x9F) to the Windows console with the help of David:

  1. The output encoding has to be set to UTF-8.
  2. The font used by the console should be something like Consolas.

Now I'd like to be able to read back text which contains special characters found in the 0x80-0x9F range (using the Windows 1252 encoding), such as the EURO sign (€):

string text = System.Console.ReadLine ();

returns null whenever I type one of the special characters. I tried naively to set the InputEncoding to UTF8:

System.Console.InputEncoding = System.Text.Encoding.UTF8;

but this does not help.

解决方案

You could set the input code page of your console application to read those special characters. There is a Win32 api called SetConsoleCP to set the input code page. In the following example I use Windows-1252 code page:

[DllImport("kernel32.dll")]
private static extern bool SetConsoleCP(uint wCodePageID);

static void Main(string[] args)
{
  SetConsoleCP((uint)1252);
  Console.OutputEncoding = Encoding.UTF8;
  System.Console.Out.WriteLine("œil"); 

  string euro = Console.In.ReadLine();

  Console.Out.WriteLine(euro);
}

EDIT:

AS L.B. suggested you could also use Console.InputEncoding = Encoding.GetEncoding(1252).

Here is the complete example without interop (note, you could also use Windows-1252 code page for output encoding:

static void Main(string[] args)
{
  Console.InputEncoding  = Encoding.GetEncoding(1252);
  Console.OutputEncoding = Encoding.GetEncoding(1252);
  System.Console.Out.WriteLine("œil"); 

  string euro = Console.In.ReadLine();

  Console.Out.WriteLine(euro);
}

END EDIT

Hope, this helps.

这篇关于如何从C#中的Windows控制台读取特殊字符(0x80 ... 0x9F)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆