读取控制台应用程序的 unicode 输出
我有控制台应用程序。用 Delphi 2010 编写。它的输出支持 Unicode。 (我为此使用了 UTF8Encode 和 SetConsoleOutputCP(CP_UTF8) )。当我从命令提示符运行该程序时,它工作正常。
现在我想读取在 Delphi 5 中创建的另一个程序的输出。我使用这个方法。但我对 unicode 字符有疑问。
有没有人建议阅读控制台应用程序的 unicode 输出。来自德尔福5?
I've console app. written in Delphi 2010. It's output is Unicode supported. (I used UTF8Encode and SetConsoleOutputCP(CP_UTF8) for this). When I run the program from command prompt it works fine.
Now I want to read the output from another program which was created in Delphi 5. I use this method. But I've problems with unicode characters.
Does anyone have a recommendation to read the unicode output of console app. from Delphi 5?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Delphi 5 确实有 unicode 支持,但只能通过 UTF-16(-LE) 编码的 WideStrings 实现。 D5 本身不支持 UTF-8。
您可以按照您已经做的方式读取 D2010 控制台应用程序的输出,尽管我会去掉 OemToAnsi 转换。 OEMToAnsi 被 OEMToChar 取代(即使在 D5 天),OEMToChar 可用于将 OEM 字符转换为 Ansi(使用各种代码页的单字节字符)或 WideString(UTF-16-LE Unicode),但它不会做任何事情解释传入的 UTF-8 字节可能会把事情搞砸。
您需要的是一组函数,可以获取从管道读取的所有“原始”utf-8 字节,并将它们转换为(UTF-16-LE 编码)WideStrings,然后您可以将其提供给可以获取的控件并显示 WideStrings。或者,您可以寻找一种能够自行执行“原始”字节解释和转换的控件,但我必须承认,我还没有看到任何控件,更不用说仍然支持 D5 了。
DIUnicode 是一个可以转换多种不同编码并且仍然支持 D5 的库: http ://www.wikitaxi.org/delphi/doku.php/products/unicode/index
Delphi 5 does have unicode support, but only through WideStrings which are UTF-16(-LE) encoded. Natively, D5 does not have UTF-8 support.
You can read the output of your D2010 console app in the way you already do, although I would take out the OemToAnsi conversion. OEMToAnsi was superseded (even in D5 days) by OEMToChar which can be used to convert OEM characters to Ansi (single byte characters using various code pages) or WideString (UTF-16-LE Unicode), but it won't do a thing to interpret the UTF-8 bytes coming in and might just mess things up.
What you need is a set of functions that can take all the "raw" utf-8 bytes you have read from the pipe and convert them to (UTF-16-LE encoded) WideStrings which you can then feed to a control that can take in and show WideStrings. Alternatively you could look for a control that does the "raw" byte interpretation and conversion all itself, but I must admit I haven't seen any let alone one that still supports D5.
A library that can convert many different encodings and still supports D5 is DIUnicode: http://www.wikitaxi.org/delphi/doku.php/products/unicode/index
使用带有 unicode 输出的 Delphi 5 时遇到两个问题。
首先是 TMemo 不支持 Unicode 字符,您需要找到另一个控件,例如 中的控件TMS Unicode 组件包。然而,这个组件包不支持 Delphi 5。
第二个问题是这部分代码:
它正在读取字符并将它们放入 PCHAR 缓冲区(D5 中每个字节单个字符),然后将其类型转换为String 是 D5 中的 AnsiString。
虽然我已经多年没有使用 D5,但我记得唯一可以在 D5 中处理 unicode 数据的类型是 WideString。
You have two problems using Delphi 5 with unicode output.
The first is TMemo does not support Unicode characters you will need to find another control, such as the ones in TMS Unicode Component Pack. However, this Component pack does not support Delphi 5.
The second problem is with this part of the code:
It is reading he characters and placing them into buffer which is a PCHAR (single character per byte in D5) Then type casting this to a String which is an AnsiString in D5.
Although I have not used D5 for years, the only type that I can remember that can handle unicode data in D5 is WideString.
我做了如下更改,效果很好:
在控制台应用程序中,我没有使用SetConsoleOutputCP(CP_UTF8)。仅使用字符串输出...
在另一个程序(Delphi 5)中,我使用 this 函数而不使用OemToChar(缓冲区,缓冲区)
I've changed somethings as follows and it works fine :
In console application, I didn't use SetConsoleOutputCP(CP_UTF8). Only use string output...
And at the other program (Delphi 5), I use this function without use OemToChar(Buffer,Buffer)