C# Save a file with Korean encoding

2019-07-14 02:28发布

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();
}

Can anyone help?

Thanks

标签: c# file encoding
4条回答
姐就是有狂的资本
2楼-- · 2019-07-14 03:01

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);
查看更多
手持菜刀,她持情操
3楼-- · 2019-07-14 03:09

You should use Encoding.Unicode - so the output will include the BOM and will be encoded in Unicode multi-byte.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-07-14 03:12

When the file is opened in a text editor it shows the encoding as ASCII..

There is nothing in the file that tells the text editor the encoding.

You need to sup0ply metadata in some way. Either by getting the user to use some "open with selected encoding" option in the text editor (if it has one), or use a different encoding in the file (eg. UTF-8 or -16 with a BOM) that includes the code points you need, and the text editor can detect.

查看更多
Emotional °昔
5楼-- · 2019-07-14 03:24

Try replacing

Encoding enc = Encoding.GetEncoding(51949);

with Encoding enc = Encoding.Unicode;

or Encoding enc = Encoding.UTF8;

查看更多
登录 后发表回答