使用以下代码定位netcoreapp2.1
:
static void Main()
{
Console.InputEncoding = Console.OutputEncoding = Encoding.UTF8;
while (true)
{
var line = Console.ReadLine();
Console.WriteLine($"Length: {line.Length}");
Console.WriteLine($"Bytes: {string.Join(", ", Encoding.UTF8.GetBytes(line).Select(b => b.ToString()))}");
Console.WriteLine(line);
}
}
当我键入что(俄语单词)时,输出为:
Length: 3
Bytes: 0, 0, 0
<<there is an empty line here>>
当我键入 Hello world 时,输出为:
Length: 11
Bytes: 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100
Hello world
如果我将输入/输出编码设置为Encoding.Unicode
,则它会按预期工作(俄语单词会像您期望的那样产生六个字节)。有什么作用?