C#使用韩文编码保存文件 [英] C# Save a file with Korean encoding

查看:221
本文介绍了C#使用韩文编码保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下代码块,该代码块使用选定的编码保存文件。在文本编辑器中打开文件时,它将显示为ASCII编码。

Have the following codeblock that saves a file with the selected encoding. When the file is opened in a text editor it shows the encoding as ASCII..

StringBuilder sb = new StringBuilder();
sb.Append(); // Lots of korean text here

Encoding enc = Encoding.GetEncoding(51949);
using (StreamWriter sw = new StreamWriter(fileName, false, enc))
{
    sw.Write(sb.ToString());
sw.Flush();
sw.Close();
}

有人可以帮忙吗?

谢谢

推荐答案

在保存文件时必须使用UnicodeEncoding ,对于unicode编码,请使用以下代码,并根据需要进行修改。
尝试以下操作:

You have to use UnicodeEncoding while saving the file, and for unicode Encoding, this the below code, and modify as per your need. try this:

 UnicodeEncoding unicode = new UnicodeEncoding();

        // Create a string that contains Unicode characters.
        String unicodeString =
            "This Unicode string contains two characters " +
            "with codes outside the traditional ASCII code range, " +
            "Pi (\u03a0) and Sigma (\u03a3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);

        // Encode the string.
        Byte[] encodedBytes = unicode.GetBytes(unicodeString);
        Console.WriteLine();
        Console.WriteLine("Encoded bytes:");
        foreach (Byte b in encodedBytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();

        // Decode bytes back to string.
        // Notice Pi and Sigma characters are still present.
        String decodedString = unicode.GetString(encodedBytes);
        Console.WriteLine();
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);

这篇关于C#使用韩文编码保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆