使用C#将UTF-8转换为ANSI [英] UTF-8 to ANSI Conversion using C#

查看:877
本文介绍了使用C#将UTF-8转换为ANSI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.NET开发人员,被要求做一个将C#中的html文件转换为ANSI的应用程序。

I'm a .NET developer and was asked to do an application that converts html files to ANSI in C#.

ANSI是必需的,因为转换后的文件将在Visual Fox Pro应用程序上运行。

ANSI is necessary because the converted files will run on a Visual Fox Pro application.

基本逻辑是

我已经尝试了以下代码: http://social.msdn.microsoft.com/Forums/ pt-BR / 026ddda3-9bd1-4502-b445-e2a1cc88345d / convert-file-from-utf8-to-ansi?forum = csharplanguage ,但是当我在editplus上对其进行检查时,该文件仍未转换为ANSI,甚至最糟糕的是,压痕都弄糟了。

I've tried this code: http://social.msdn.microsoft.com/Forums/pt-BR/026ddda3-9bd1-4502-b445-e2a1cc88345d/convert-file-from-utf8-to-ansi?forum=csharplanguage but when I checked it on editplus the file is still not converted to ANSI and even worst the indentation it's all messed up.

我应该做的就是像editplus一样转换文件,它保留文档缩进,并且可以将任何文件从UTF8转换为ANSI。

What I should do is to convert a file like editplus does, it preserves the document indentation and can convert any file from UTF8 to ANSI.

整个要点是,我正在处理数百个html文件,所以我不能只使用文本编辑器一个接一个地完成它。

The whole point is that I'm working with hundreds of html files, so I can't just do it one by one using a text editor.

如何完成转换?

有没有办法像Editplus一样转换并保留缩进?

Is there a way to convert it and preserve the indentation like editplus does?

对于特殊字符,例如:ã,ão,é,í...,我正在转换前对其进行更正。这是正确的方法吗?

For the special characters like: "ã, ão, é, í..." I'm correcting it before the conversion. Is this the right approach?

推荐答案

使用默认编码代替ASCII:

Use Default Encoding instead of ASCII:

StreamReader sr = new StreamReader(infile);  
StreamWriter sw = new StreamWriter(outfile, false, Encoding.Default);  

// invoke the ReadToEnd method
sw.WriteLine(sr.ReadToEnd());  

sw.Close();  
sr.Close(); 

这篇关于使用C#将UTF-8转换为ANSI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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