将UTF-8字符串存储在UnicodeString中 [英] Storing UTF-8 string in a UnicodeString

查看:150
本文介绍了将UTF-8字符串存储在UnicodeString中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi 2007中,您可以在WideString中存储UTF-8字符串,然后将其传递到Win32函数,例如

In Delphi 2007 you can store a UTF-8 string in a WideString and then pass that onto a Win32 function, e.g.

var
  UnicodeStr: WideString;
  UTF8Str: WideString;
begin
  UnicodeStr:='some unicode text';
  UTF8Str:=UTF8Encode(UnicodeStr);
  Windows.SomeFunction(PWideChar(UTF8Str), ...)
end;

Delphi 2007不会干扰UTF8Str的内容,即留作UTF-8编码字符串存储在WideString中。

Delphi 2007 does not interfere with the contents of UTF8Str, i.e. it is left as a UTF-8 encoded string stored in a WideString.

但是在Delphi 2010中,我正在努力寻找一种方法来做同样的事情,即在WideString中存储UTF-8编码的字符串而不会自动从UTF-8转换。我不能传递一个指向UTF-8字符串(或RawByteString)的指针,例如以下显然不起作用:

But in Delphi 2010 I'm struggling to find a way to do the same thing, i.e. store a UTF-8 encoded string in a WideString without it being automatically converted from UTF-8. I cannot pass a pointer to a UTF-8 string (or RawByteString), e.g. the following will obviously not work:

var
  UnicodeStr: WideString;
  UTF8Str: UTF8String;
begin
  UnicodeStr:='some unicode text';
  UTF8Str:=UTF8Encode(UnicodeStr);
  Windows.SomeFunction(PWideChar(UTF8Str), ...)
end;


推荐答案

您原来的Delphi 2007代码正在转换UTF-8使用ANSI代码页将其字符串扩展到最大范围。要在Delphi 2010中做同样的事情,您应该使用SetCodePage与Convert参数false。

Your original Delphi 2007 code was converting the UTF-8 string to a widestring using the ANSI codepage. To do the same thing in Delphi 2010 you should use SetCodePage with the Convert parameter false.

var
  UnicodeStr: UnicodeString;
  UTF8Str: RawByteString;
begin
  UTF8Str := UTF8Encode('some unicode text');
  SetCodePage(UTF8Str, 0, False);
  UnicodeStr := UTF8Str;
  Windows.SomeFunction(PWideChar(UnicodeStr), ...)

这篇关于将UTF-8字符串存储在UnicodeString中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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