简体   繁体   English

将文本文件从已知(1252)编码转换为UTF8文件

[英]Convert text file from known (1252) encoding to UTF8 File

I have a file which has text with windows-1252 encoding. 我有一个包含Windows-1252编码文本的文件。 How to convert it into UTF8 file format? 如何将其转换为UTF8文件格式?

The Encoding class supports conversions. Encoding类支持转换。

byte[] asciiBytes = File.ReadAllBytes("C:\\ascii.txt");

Encoding ASCII_1252 = Encoding.GetEncoding("windows-1252");

byte[] utf8Bytes = Encoding.Convert(ASCII_1252, Encoding.UTF8, asciiBytes);

File.WriteAllBytes("C:\\utf8.txt", utf8Bytes);

Note that GetEncoding() relies on the underlying platform to support most code pages as explained here: http://msdn.microsoft.com/en-us/library/t9a3kf7c(v=vs.100).aspx 请注意,GetEncoding()依赖基础平台来支持大多数代码页,如下所述: http : //msdn.microsoft.com/zh-cn/library/t9a3kf7c (v=vs.100) .aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM