A
A
Alexander Abaishvili2022-02-26 21:02:40
C++ / C#
Alexander Abaishvili, 2022-02-26 21:02:40

Is it possible to use Japanese characters in the console?

Good day to all! Thank you. that drew attention to this issue. What is the problem:
I have an array whose values ​​come from a file:

string[] Symbols = File.ReadAllLines("Symbols.txt");

The file contains Japanese characters. And when a character is output to the console, instead of a character, it shows a question mark. I searched on other forums and did not find it, most likely I was looking badly. That's why I'm asking for help here. How to output characters to the console?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dasolod, 2022-02-26
@Alexandr03

You need to set the encoding for what is output to the console, and switch the console itself to display code page 932.

public class Program
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetConsoleOutputCP(uint wCodePageID);

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

        private static void Main(string[] args)
        {
            SetConsoleOutputCP(932);
            SetConsoleCP(932);
            Console.OutputEncoding = Encoding.Unicode;
            Console.WriteLine(@"一	人	七	八	九	十	入	二	三	上
            下   大   女   万   土   山   千   川   子   小
            口   手   五   日   六   分   中   円   月   午");
        }
    }

V
Vasily Bannikov, 2022-02-27
@vabka

The file contains Japanese characters. And when a character is output to the console, instead of a character, it shows a question mark.

Either the problem is in the console encoding, or the problem is in the fonts.
To force the application to change the encoding - there is
System.Console.OutputEncoding = Encoding.UTF8;
Also possible to do this from the outside: on Windows - the command chcp 65001.
It seems that you can even globally make all applications work with unicode (in the settings), but I have a lot of applications then I start to behave badly.
And the user will have to deal with fonts.
You should also make sure that when reading the file, the same encoding is used that was used when writing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question