Question console does not display Chinese input string

ying

New member
Joined
Nov 24, 2020
Messages
2
Programming Experience
Beginner
Hello,

as my title says: on Windows 10, my console (cmd prompt) does not display Chinese input string by Console.ReadLine (but it displays Chinese non-input strings correctly).
Here is my problematic code-chunk:
C#:
using System;
using System.IO;
using System.Text;

namespace 字符
{
    class Program
    {
        static void Main(string[] args)
        {   
           
            Console.InputEncoding = Encoding.UTF8;
            string Name = "字符人";
            int Age;
            Age = 1;

            Console.WriteLine("你好!我是一个"+ Name + "。");
            Console.WriteLine("我现在" + Age + "岁了。");
            Console.WriteLine("这是现在的我:");
            Console.WriteLine(" ---------");
            Console.WriteLine("|    |    |");
            Console.WriteLine("|----+----|");
            Console.WriteLine("|    |    |");
            Console.WriteLine(" ---------");

            string Nameuser, Name1, 颜色, 高度, 兴趣爱好, 性格特点;
           
            Console.Write("你叫什么名字:");
           
            Nameuser = Console.ReadLine();
            Console.WriteLine("你好,"+ Nameuser + "!"); 
(etc.)

The output in console (cmd prompt) looks like this:
C#:
你好!我是一个字符人。
我现在1岁了。
这是现在的我:
---------
|    |    |
|----+----|
|    |    |
---------
你叫什么名字:王先生
你好,   !
你也帮我取个名字:
As you can see, between 你好 and !there are three whitespaces. I have specified the encoding for console input, therefore I do not understand why the input string is not displayed correctly.
Thanks in advance for Your help!
Cheers XD
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,994
Location
Chesapeake, VA
Programming Experience
10+
I don't have a machine setup for Chinese locale, but part of me is suspicious of the your line of code where you wrote Console.InputEncoding = Encoding.UTF8; yet you say that you are entering Chinese characters. In my mind, if the input encoding is UTF8, you should be not be entering Unicode Chinese characters. You should be entering UTF8 8-bit characters that represent the Unicode Chinese characters.
 

ying

New member
Joined
Nov 24, 2020
Messages
2
Programming Experience
Beginner
Dear Skydiver, thank you for your advice. Actually I added that line after the problem first occurred, trying to solve it. As far as I know, UTF8 should be able to deal with Chinese just fine. However, I would welcome any suggestions about the encoding. Any ideas?
Thanks!
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,994
Location
Chesapeake, VA
Programming Experience
10+
Looks to be good ideas in this SO response:
 
Top Bottom