(reproduced) dotnet Core Chinese garbled codepages

Source: Internet
Author: User
Tags dotnet volatile

Introduction

Reprinted from: http://www.jianshu.com/p/1c9c59c5749a

Reference:. Net Core Console output garbled in Chinese

In the above I consulted some of the CLI's source code, the idle to continue to turn over the codes, the universe has its own providence, in the endless code, I saw such a comment

default, .NET Core doesn‘t have all code pages needed for Console apps.// see the .NET Core Notes in https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx

Dotnet Core team's idea is good, provide a relatively small assembly, the code farmers to their own needs to add the need to rely on their own, you can generate a smaller assembly, fully agree. But, I guess someone must have crashed.

Is someone falling?

Google for a moment to produce the following results

I do not know what I expected, the winning brothers are quite a lot, but also early on the great God of the matter has been described, and has already given a solution in its text, the great God of the article is not in the. NET core to R2 Chinese encoding is not supported? (http://www.cnblogs.com/artech/archive/2016/05/18/5507092.html)
Someone's going to ask, what are you doing with that crap?

It's not over yet!

Why is it not finished? Because of the comments on the great God article, a brother has not climbed up in the pit. Chuan-Cool can't die, does it?
According to the man, that's what happened.

        Console.WriteLine("你好, 世界!");        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);        Console.WriteLine("你好, 世界!");

According to the great God article, registered codepages code is UTF-8, Chinese should not garbled, but the fact is

Is the great god wrong? Of course not!

        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Under normal circumstances this code is sufficient to solve the problem, according to the CLI note, for the console program, and did not add all the code page, and the comments are given in the Microsoft Official solution is to add codepage DLL assembly, and add the above sentence code.
This time I found another great God's blog, there is a different solution

    public static void Main(string[] args)    {        Console.OutputEncoding = System.Text.Encoding.UTF8;//第一种方式:指定编码 //Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);//第二种方式 Console.Read(); }

Since there is a plan, Chuan-cool definitely want to try.

        Console.WriteLine("你好, 世界!");        Console.OutputEncoding=Encoding.UTF8;        //Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);        Console.WriteLine("你好, 世界!");

The great God is the great God, this recruit spirit!

However, the output of the second line has a small problem, interested in children's shoes research, do not repeat this.

Let me take you to fly!

According to Chuan cool style, the time to turn the code! Now that the problem is in the console, look for the console code. Interested can get the code here. Let's find Console.cs this file and we can see the overloads of the corresponding WriteLine method.

    [MethodImplAttribute(MethodImplOptions.NoInlining)]    public static void WriteLine(String value)    {        Out.WriteLine(value); }

What a ghost it is to see out again.

    public static TextWriter Out    {        get { return Volatile.Read(ref s_out) ?? EnsureInitialized(ref s_out, () => CreateOutputWriter(OpenStandardOutput())); } }

Then we find out that S_out is the TextWriter for the output.
And a new one is created at the time of the first call.

    private static TextWriter CreateOutputWriter(Stream outputStream)    {        return SyncTextWriter.GetSynchronizedTextWriter(outputStream == Stream.Null ?            StreamWriter.Null : new StreamWriter( stream: outputStream, encoding: new ConsoleEncoding(OutputEncoding), // This ensures no prefix is written to the stream. bufferSize: DefaultConsoleBufferSize, leaveOpen: true) { AutoFlush = true }); }

So it seems that the TextWriter encoding is the use of outputencoding, then we practice, this initial outputencoding exactly what.

Since it is UTF-8 why still garbled? This matter should also from the Microsoft's that comment, people said, the default is not add codepage, even if you are now UTF-8 code, but there is no codepage in the program, of course, no way to deal with. Let's take a look at the source code of outputencoding.

    PublicStatic Encoding outputencoding {get {Return Volatile.read (Ref s_outputencoding)?? Ensureinitialized (Ref s_outputencoding, () = consolepal.outputencoding); }set {Checknonnull (Value"Value");Lock (Internalsyncobject) {Set the terminal console encoding. Consolepal.setconsoleoutputencoding (Value);Before changing the code page we need to flush the data//if out hasn ' t been redirected. Also, the next call to //s_out reinitialize the console code page. if (Volatile.read (ref s_out)! = null &&!s_ isouttextwriterredirected) {s_out. Flush (); Volatile.write (ref s_out, null);} if (Volatile.read (ref s_error)! = null &&!s_iserrortextwriterredirected) {S_error. Flush (); Volatile.write (ref s_error, null);} Volatile.write (ref s_outputencoding, (Encoding) value. Clone ()); } } }

The question, I guess, is in the phrase consolepal.setconsoleoutputencoding (value).
I believe you can remember that the Chinese display of the console after I had the dominant set outputencoding for UTF-8 was normal. That is, after the set action is made to outputencoding, the console window is forced to introduce the codepage file.

Summary

Compare the two solutions, in fact, there are essentially different, setconsoleoutputencoding is for this console instance encoding settings, and Registerprovider is the current assembly to add codepage.
The reason Encoding.registerprovider (codepagesencodingprovider.instance) does not work is because the previous sentence Console.WriteLine ("Hello, world!") The command-line window has been initialized for encoding. Execution Encoding.registerprovider (codepagesencodingprovider.instance) does not work on the command-line window, here is an experiment, I believe you can understand what happened in the middle.
First, repeat the code that you just started.

        Console.WriteLine("你好, 世界!");        // Console.OutputEncoding=Encoding.UTF8;        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);        Console.WriteLine("你好, 世界!");

The result is this.

Then execute this sentence for the command line Console.outputencoding=encoding.utf8;. Look at the results again.


In fact, the Registerprovider method at this time is meaningless, because the command line has loaded the codepage. That's it, end it!

Welcome to discuss, if you feel wrong or wrong place also hope you can correct me, I will try to write some high-quality articles.

(reproduced) dotnet Core Chinese garbled codepages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.