在 Delphi 版本中处理 Unicode 字符串 <= 2007 [英] Handling a Unicode String in Delphi Versions &lt;= 2007

查看:26
本文介绍了在 Delphi 版本中处理 Unicode 字符串 <= 2007的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:这个问题与 2009 年以下的 Delphi 版本有关(即没有内置的 Unicode 支持).我有一个规范要求我通过 TCP 连接传输 Unicode 编码的字符串,但我没有 Delphi 2009.

Background: This question relates to versions of Delphi below 2009 (ie without Unicode support built in). I have a specification that requires me to transmit a Unicode encoded string over a TCP connection but I do not have Delphi 2009.

问题 是否有一个函数或非常小的库(我不需要太多的批量)可以在通过网络发送之前立即将单个字符串编码为 UTF-8?作为我问题的第二部分:如果有 UTF-8 编码的字符串作为响应被发回,我想我将需要另一个函数将其恢复为 Delphi 字符串格式.我以这种方式理解这种 Unicode 支持的局限性.

Question Is there a single function or very small library (I don't need too much bulk) that I can use to encode a single string into UTF-8 immediately prior sending over the wire? As a second part of my question: if there are UTF-8 encoded strings being sent back as a response, I guess I would then need another function to get it back into a Delphi string format. I understand the limitations of such Unicode support in this way.

推荐答案

Delphi 2009 之前的Delphi 版本do内置了Unicode 支持.WideString 类型从 Delphi 4 开始就可用了,我想,也许更早.WideString 不如新的 UnicodeString 类型好,但它仍然包含 16 位 Unicode 字符,您可以将其类型转换为 PWideChar 将字符串发送到 Unicode API 函数.Windows 单元声明了 API 函数的大部分宽"版本,如果您发现某些函数缺失,没有什么可以阻止您自己声明其他函数.

Delphi versions prior to Delphi 2009 do have Unicode support built in. The WideString type has been available since Delphi 4, I think, maybe earlier. WideString isn't as nice as the new UnicodeString type, but it still holds 16-bit Unicode characters, and you can type-cast it to PWideChar to send strings to Unicode API functions. The Windows unit declares most of the "wide" versions of the API functions, and there's nothing to stop you from declaring other functions yourself if you find some missing.

以前的版本没有 VCL 中的 Unicode 支持.为此,您可以使用 Tnt Unicode 控件.他们曾经是免费的.看起来有几个地方仍然提供最新的免费版本:(1), (2).

What prior versions don't have is Unicode support in the VCL. For that, you can use the Tnt Unicode controls. They used to be free. Looks like there are a few places where the latest free version is still available: (1), (2).

JCL 有几个用于处理的单元统一码.JclWideStrings 单元主要具有轻量级实用功能.JclUnicode 单元更完整,但它也包含一个相当大的资源,用于确定所有 Unicode 字符的字符属性.

The JCL has a couple of units for working with Unicode. The JclWideStrings unit has mostly light-weight utility functions. The JclUnicode unit is more complete, but it also includes a sizable resource for determining character properties of all Unicode characters.

使用 JCL,您可以选择一些类来保存 WideString 值列表.我认为 Delphi 7 甚至为此提供了一个类.

With the JCL you have a few choices for classes to hold lists of WideString values. I think Delphi 7 even comes with a class for that.

不要以为没有 Delphi 2009 就不能编写 Unicode 程序.

Don't think that just because you don't have Delphi 2009 you can't write a Unicode program.

如果您有一个 WideString 值,并且想将其编码为 UTF-8,则调用 Utf8Encode 函数.如果您的 Delphi 版本声明了该类型,它将返回一个 AnsiString 值,或者可能返回 Utf8String.不过,它与 Delphi 2009 的 Utf8String 类型不同.Delphi 2009 将在赋值语句中自动转换为UnicodeStringAnsiString(x),反之亦然.之前的版本只有一个 AnsiString 类型,因此您需要自己跟踪哪些变量保存 UTF-8 数据,哪些变量保存 Ansi 数据.(变量和参数名称上的匈牙利符号可以帮助您跟踪.)当然,还有一个 Utf8Decode 函数用于将 UTF-8 数据转换回 WideString.

If you have a WideString value, and you want to encode it as UTF-8, then call the Utf8Encode function. It will return an AnsiString value, or possibly Utf8String, if your Delphi version declares that type. It's not the same as Delphi 2009's Utf8String type, though. Delphi 2009's will automatically convert to UnicodeString or AnsiString(x) and vice versa in assignment statements. Prior versions just have a single AnsiString type, so you need to keep track for yourself which variables hold UTF-8 data and which hold Ansi data. (Hungarian notation on your variable and parameter names can help you keep track.) And of course, there's also a Utf8Decode function for converting UTF-8 data back to WideString.

要处理其他字符编码,您需要查看Open XML,一个用于 Delphi 的免费 XML 库.作为其 XML 处理的一部分,它支持在 70 种不同编码之间进行转换.

For handling other character encodings, you want to check out Open XML, a free XML library for Delphi. As part of its XML handling, it has support for converting between 70 different encodings.

这篇关于在 Delphi 版本中处理 Unicode 字符串 <= 2007的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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