在Delphi 7中进行字符串转换 [英] Widestring to string conversion in Delphi 7

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

问题描述



我想使用这个功能将unicode字符串转换为ANSI:

  function convertU(ws:widestring):string; 
begin
result:= string(ws);
结束

我也使用此代码设置正确的代码页转换。

 初始化
SetThreadLocale(GetSystemDefaultLCID);
GetFormatSettings;

它在VCL主线程中运行良好,但不在TThread,
中,一些问题标记为'?',因为 convertU 的结果。



为什么不在TThread?

解决方案

调用 SetThreadLocale() / code> block对 TThread 没有影响。如果要设置线程的区域设置,则必须在 TThread.Execute()内调用 SetThreadLocale()方法。



更好的选择是根本不依赖于 SetThreadLocale()。通过直接调用 WideCharToMultiByte()进行自己的转换,以便您可以指定要转换的特定Ansi代码页。


my app is a non-unicode app written in Delphi 7.

I'd like to convert unicode strings to ANSI with this function :

function convertU(ws : widestring) : string;
begin
  result := string(ws);
end;

I use also this code to set the right codepage to convert.

initialization
  SetThreadLocale(GetSystemDefaultLCID);
  GetFormatSettings;

It works great in the VCL main thread but not in a TThread, where I get some questions marks '?' as result of function convertU.

Why not in a TThread ?

解决方案

Calling SetThreadLocale() inside of an initialization block has no effect on TThread. If you want to set a thread's locale, you have to call SetThreadLocale() inside of the TThread.Execute() method.

A better option is to not rely on SetThreadLocale() at all. Do your own conversion by calling WideCharToMultiByte() directly so you can specify the particular Ansi codepage to convert to.

这篇关于在Delphi 7中进行字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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