本文介绍了在.NET控制台应用程序的越南字符(UTF-8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写下了UTF8字符串(越南)到C#控制台,但没有成功。在windows进出口运行7.

我试图用该字符串转换Encoding类为char []为byte [],然后转换为字符串,但没有任何帮助,该字符串直接输入弗朗数据库。

下面是一些例子

它不显示像特殊字符:DJ或U ......相反,它显示出来,用编码类差远了。

?。

有谁可以尝试了这一点,或知道这个问题?

感谢您


我的code       静态无效的主要(字串[] args)        {            XDataContext _new =新XDataContext();            Console.OutputEncoding = Encoding.GetEncoding(UTF-8);            字符串srcString = _new.Posts.First()TITLE;

  Console.WriteLine(srcString);
        //转换为UTF-16连接codeD源字符串转换为UTF-8和ASCII。
        byte []的UTF8字符串= Encoding.UTF8.GetBytes(srcString);
        byte []的asciiString = Encoding.ASCII.GetBytes(srcString);

        //写UTF-8和ASCII EN codeD字节数组。
        Console.WriteLine(UTF-8字节:{0},BitConverter.ToString(UTF8字符串));
        Console.WriteLine(ASCII字节:{0},BitConverter.ToString(asciiString));


        //转换UTF-8和ASCII EN codeD字节回UTF-16连接codeD
        //字符串和写入。
        Console.WriteLine(UTF-8文本:{0},Encoding.UTF8.GetString(UTF8字符串));
        Console.WriteLine(ASCII文本:{0},Encoding.ASCII.GetString(asciiString));

        Console.WriteLine(Encoding.UTF8.GetString(UTF8字符串));
        Console.WriteLine(Encoding.ASCII.GetString(asciiString));
    }
 

和这里是出色的输出

 芽báoÄ'i厦»™Ibáo随感¢ñ
UTF-8的字节:4E-68-C 3 A0-20-62-C 3 A1-6F -20- C4-91-69-20-68-E1-BB-99-69-20-62-C3-
A1-6F-20-58-75 -C 3  -  A2-6E
ASCII字节:4E-68-3F-20-62-3F-6F-20-3F-69-20-68-3F-69-20-62-3F-6F-20-58-75-3F-
6E
UTF-8文本:芽báoÄ'i厦»™Ibáo随感¢ñ
ASCII文本:新罕布什尔州? B 2 O?I H?I B 2 O徐3 N
芽báoÄ'i厦»™Ibáo随感¢ñ
新罕布什尔州? B 2 O?I H?I B 2 O徐3 N


preSS任意键继续。 。 。
 

解决方案

 类节目
{
    [的DllImport(KERNEL32.DLL)
    静态外部布尔SetConsoleOutputCP(UINT W codePageID);

    静态无效的主要(字串[] args)
    {
        SetConsoleOutputCP(65001);
        Console.OutputEncoding = Encoding.UTF8;
        Console.WriteLine(测试,тест,τεστ,←↑→↓ΠΣ√∞①②③④,白越南川川LOC);
        到Console.ReadLine();
    }
}
 

输出截图(使用索拉或具有上述所有的字母组成的字体):

Im trying to write down an UTF8 string (Vietnamese) into C# Console but no success. Im running on windows 7.

I tried to use the Encoding class that convert string to char[] to byte[] and then to String, but no help, the string is input directly fron the database.

Here is some example

It does not show the special character like : Đ or ứ... instead it show up ?, much worse with the Encoding class.

Does anyone can try this out or know about this problem?

Thank you


My code static void Main(string[] args) { XDataContext _new = new XDataContext(); Console.OutputEncoding = Encoding.GetEncoding("UTF-8"); string srcString = _new.Posts.First().TITLE;

        Console.WriteLine(srcString);
        // Convert the UTF-16 encoded source string to UTF-8 and ASCII.
        byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
        byte[] asciiString = Encoding.ASCII.GetBytes(srcString);

        // Write the UTF-8 and ASCII encoded byte arrays.
        Console.WriteLine("UTF-8  Bytes: {0}", BitConverter.ToString(utf8String));
        Console.WriteLine("ASCII  Bytes: {0}", BitConverter.ToString(asciiString));


        // Convert UTF-8 and ASCII encoded bytes back to UTF-16 encoded
        // string and write.
        Console.WriteLine("UTF-8  Text : {0}", Encoding.UTF8.GetString(utf8String));
        Console.WriteLine("ASCII  Text : {0}", Encoding.ASCII.GetString(asciiString));

        Console.WriteLine(Encoding.UTF8.GetString(utf8String));
        Console.WriteLine(Encoding.ASCII.GetString(asciiString));
    }

and here is the outstanding output

Nhà báo đi hội báo Xuân
UTF-8  Bytes: 4E-68-C3-A0-20-62-C3-A1-6F-20-C4-91-69-20-68-E1-BB-99-69-20-62-C3-
A1-6F-20-58-75-C3-A2-6E
ASCII  Bytes: 4E-68-3F-20-62-3F-6F-20-3F-69-20-68-3F-69-20-62-3F-6F-20-58-75-3F-
6E
UTF-8  Text : Nhà báo đi hội báo Xuân
ASCII  Text : Nh? b?o ?i h?i b?o Xu?n
Nhà báo đi hội báo Xuân
Nh? b?o ?i h?i b?o Xu?n


Press any key to continue . . .
解决方案
class Program
{
    [DllImport("kernel32.dll")]
    static extern bool SetConsoleOutputCP(uint wCodePageID);

    static void Main(string[] args)
    {
        SetConsoleOutputCP(65001);
        Console.OutputEncoding = Encoding.UTF8;
        Console.WriteLine("tést, тест, τεστ, ←↑→↓∏∑√∞①②③④, Bài viết chọn lọc");
        Console.ReadLine();
    }
}

Screenshot of the output (use Consolas or another font that has all the above characters):

这篇关于在.NET控制台应用程序的越南字符(UTF-8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 22:55
查看更多