Delphi - 在程序中存储 WideStrings
过去我使用 INI 文件来存储 unicode 文本,但现在我需要在可执行文件中存储 unicode 文本。我怎样才能做到这一点?
我想存储这些字母:
āčēūīšķļņž
In the past I used INI-Files to store unicode text, but now I need to store unicode text in the executable. How can I achieve this?
I want to store these letters:
āčēūīšķļņž
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你想保存 Unicode INI 文件,那么你可以尝试以下代码。这些文件以 UTF8 编码 保存。
您也可以看一下这个 Unicode 库,您可以在其中找到很多辅助函数。
64 位 Windows 7 Enterprise SP 1 上使用 Delphi 2007
If you want to save the Unicode INI files then you might try the following code. The files are saved in UTF8 encoding.
Also you might take a look at this Unicode library where you can find a lot of helper functions.
with Delphi 2007 on 64-bit Windows 7 Enterprise SP 1
如果您确实需要使用 Delphi 7,则有一些变体:
将字符串存储在链接到可执行文件的资源中。
将字符串存储在大备忘录或相同的东西中,位于全局数据模块或任何其他可视或非可视组件上,并通过索引访问它。这是可能的,因为 Delphi 资源中的字符串以 XML 编码的形式存储。例如,您的符号示例
āčēūīšķļņž
将存储为āčēūīš& ;#311;ļņž
将 XML 编码或 Base64 编码的字符串存储在代码内的字符串常量中。
对于字符串转换,您可以使用 EncdDecd.pas 、 xdom.pas 或 System.pas 的某些函数,例如 UTF8Encode/UTF8Decode。
要在 Delphi 表单中显示和编辑 Unicode 字符串,您可以使用特殊的 Unicode 控件集,例如 TNT Unicode Controls 或对原始 Delphi 控件进行子类化并自行执行一些其他解决方法,如本节中所述摘自 TntControls.pas 中的注释(TNT Unicode 控件的一部分):
If you definitely need to use Delphi 7 there are some variants:
Store strings in resources linked to executable file.
Store strings in big memo or same thing, located on global data module or any other visual or non-visual component and access it by index. It's possible because strings in Delphi resources stored in XML-encoded form. E.g. your symbols example
āčēūīšķļņž
will be stored asāčēūīšķļņž
Store XML-encoded or Base64-encoded strings in string constants inside your code.
For string conversion you can use EncdDecd.pas , xdom.pas or some functions of System.pas like UTF8Encode/UTF8Decode.
To display and edit Unicode strings in Delphi forms you can use special set of Unicode controls like TNT Unicode Controls or subclass original Delphi controls and do some other workarounds by yourself, like described in this excerpt from comments in TntControls.pas (part of TNT Unicode Controls):
做
Do
很简单,想法就是找到一个非可视组件,它可以存储文本并将您的文本存储在那里。希望这样的组件还可以为您提供一个编辑器来在设计时编辑文本。
有一个组件调用
FormResource
可以做到这一点。我使用 TUniScript。我相信还有其他类似的组件。但是,我没有从标准库中找到可用的组件。Simple, the idea is to find a non-visual component, which can store text and store your text there. Prefer that such component can also provide you an editor to edit the text in design time.
There is a component call
FormResource
which can do this. I useTUniScript
. I believe there are other similar components. However, I did not find a usable component from the standard library.Widestring(#$65E5#$672C)
方法不起作用,因为 Delphi 7 不希望#
超过一个字节,因此结果是当价格超过 255 或 $FF 时,远不是您所期望的。当知道您需要在源代码中使用
Widestring
时,可以使用另一种方法WideChar($65E5)+ WideChar($672C)
在源代码中存储单个 Unicode 代码点。赋值的开始(也可以是空文字),以便编译器了解您想要哪种数据类型:看起来很麻烦,但在 Delphi 7 中肯定有您的 UTF-16 文本。
或者,将常量存储在UTF-8,ASCII 安全 - 这样您就可以轻松使用
#
。优点之一是,在源代码中编写要简单得多。一个缺点是,您永远不能直接使用该常量,而必须先将其转换为 UTF-16:The approach
Widestring(#$65E5#$672C)
does not work, because Delphi 7 just doesn't expect more than one byte for the#
, so the outcome is by far not what you expect when going above 255 or $FF.Another approach
WideChar($65E5)+ WideChar($672C)
can be used to store single Unicode codepoints in your source code when knowing that you need to have aWidestring
at the start of the assignment (which can also be an empty literal) so the compiler understands which datatype you want:Looks cumbersome, but surely has your UTF-16 texts in Delphi 7.
Alternatively, store your constants in UTF-8, which is ASCII safe - that way you can use
#
easily. One advantage is, that it's a lot less cumbersome to write in your source code. One disadvantage is, that you can never use the constant directly, but have to convert it to UTF-16 first: