如何使用 Delphi 7 将文本文件从 ANSI 转换为 UTF-8?
我用 Delphi 7 编写了一个程序,用于搜索硬盘上的 *.srt
文件。 该程序在备忘录中列出这些文件的路径和名称。 现在我需要将这些文件从 ANSI 转换为 UTF-8,但我还没有成功。
I written a program with Delphi 7 which searches *.srt
files on a hard drive. This program lists the path and name of these files in a memo. Now I need convert these files from ANSI to UTF-8, but I haven't succeeded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Utf8Encode 函数采用 WideString 字符串作为参数并返回 Utf-8 字符串。
样本:
The Utf8Encode function takes a WideString string as parameter and returns a Utf-8 string.
Sample:
看看 GpTextStream ,它看起来像与 Delphi 7 一起工作。它有能力在旧版本的 Delphi 中读取/写入 unicode 文件(尽管可以与 Delphi 2009 一起使用)并且应该有助于您的转换。
Take a look at GpTextStream which looks like it works with Delphi 7. It has the ability to read/write unicode files in older versions of Delphi (although does work with Delphi 2009) and should help with your conversion.
请在开始编码之前阅读整个答案。
问题的正确答案 - 这并不容易 - 基本上由树形步骤组成:
但是,此解决方案将返回包含输入 ANSI 字符串的 UTF-8 字符串,这可能不是解决问题的最佳方法,因为当 ANSI 函数返回文件名时,文件名可能已经损坏,因此正确的文件名不保证。
解决问题的正确方法要复杂得多:
如果您想确保您的文件名列表完全干净,您必须确保它不会被删除完全转换为 ANSI。 您可以通过显式使用文件处理 API 的“W”版本来完成此操作。 在这种情况下 - 当然 - 您不能使用 TFileStream 和其他 ANSI 文件处理对象,而是直接调用 Windows API。
这并不那么难,但是如果您已经有一个基于 TFileStream 等构建的复杂框架,@ss 可能会有点痛苦。 在这种情况下,最好的解决方案是创建一个使用适当 API 的 TStream 后代。
我希望我的回答可以帮助您或任何需要处理同样问题的人。 (不久前我不得不这么做。)
Please read the whole answer before you start coding.
The proper answer to question - and it is not the easy one - basically consist of tree steps:
However this solution will return an UTF-8 string containing the input ANSI string, this probably is not the best way to solve your problems, since the file names may already be corrupted when the ANSI functions returned them, so proper file names are not guaranteed.
The proper solution to your problem is ways more complicated:
If you want to be sure that your file name list is exactly clean, you have to make sure it won't get converted to ANSI at all. You can do this by explicitly using the "W" version of the file handling API's. In this case - of course - you can not use TFileStream and other ANSI file handling objects, but the Windows API calls directly.
It is not that hard, but if you already have a complex framework built on e.g. TFileStream it could be a bit of a pain in the @ss. In this case the best solution is to create a TStream descendant that uses the appropriate API's.
I hope my answer helps you or anyone who has to deal with the same problem. (I had to not so long ago.)
我只做了这个:
Verified with Notepad++ UTF8 without BOM
I did only this:
Verified with Notepad++ UTF8 without BOM
你指的是ASCII吗?
ASCII 向后兼容 UTF-8。
http://en.wikipedia.org/wiki/UTF-8
Did you mean ASCII?
ASCII is backwards compatible with UTF-8.
http://en.wikipedia.org/wiki/UTF-8