首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将UnicodeString复制到C++Builder Android应用程序中的wchar_t数组?

如何将UnicodeString复制到C++Builder Android应用程序中的wchar_t数组?
EN

Stack Overflow用户
提问于 2020-06-03 16:42:33
回答 1查看 516关注 0票数 0

我正在使用C++Builder 10.3力拓为安卓开发一个多平台应用程序。

我有如下一系列数据:

代码语言:javascript
复制
typedef struct recordstruct
{
    bool shop;
    bool bought;
    wchar_t description[80];
} recordtype;

recordtype MasterItems[MAXITEMS]=
{
    false,false,L"Apples",
    false,false,L"Apricots",
    false,false,L"Avocado",
...
...
};

我已经将其复制到一个TEdit中,并希望将该值返回到MasterItems数组。

我曾经用过c_str()mbstowcs()strcpy()/wcscpy()等。

请问我怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-03 19:15:22

UnicodeString是所有平台上的UTF-16编码字符串.但是,wchar_t是一种16位类型,仅用于Windows上的UTF-16数据。在其他平台上,wchar_t是一种32位类型,用于UTF-32数据。

这在Embarcadero的DocWiki中有记录:

T关于macOS和iOS

(Android也包括在内)

在macOS和iOS上,char16_t并不等同于wchar_t (就像在Windows上一样):

  • 在Windows上,wchar_tchar16_t都是双字节字符.
  • 然而,在macOSiOS和Android上,wchar_t是一个4字节的字符.

因此,要声明UTF-16常量字符串,在Windows上使用Lu前缀,而在macOS、iOS、和Android上使用u前缀。

Windows上的示例:

UnicodeString(L"Text"), UnicodeString(u"Text")

macOS,iOS,和Android的例子

UnicodeString(u"Text")

然而,在macOS、iOS、和Android上使用字符串前缀并不是错误的。在这种情况下,UTF-32常量字符串被转换为UTF-16字符串.

为便于移植,请使用_D宏编写相应以Lu作为前缀的常量字符串。示例:

UnicodeString(_D("Text"))

为了确保在所有平台上使用UTF-16,System::WideChar类型是wchar_t在Windows上的别名,在其他平台上是char16_t的别名。UnicodeStringWideChar元素的容器。

因此,如果对数组使用wchar_t,那么在非Windows平台上,首先需要在运行时将UnicodeString转换为UTF-32,例如使用RTL的UnicodeStringToUCS4String()函数,然后才能将数据复制到数组中,例如:

代码语言:javascript
复制
typedef struct recordstruct
{
    bool shop;
    bool bought;
    wchar_t description[80];
} recordtype;

recordtype MasterItems[MAXITEMS]=
{
    false,false,L"Apples",
    false,false,L"Apricots",
    false,false,L"Avocado",
    ...
};

...

#if defined(WIDECHAR_IS_WCHAR) // WideChar = wchar_t = 2 bytes

StrLCopy(MasterItems[index].description, Edit1->Text.c_str(), std::size(MasterItems[index].description)-1); // -1 for null terminator

/* or:
UnicodeString s = Edit1->Text;
size_t len = std::min(s.Length(), std::size(MasterItems[index].destination)-1); // -1 for null terminator
std::copy_n(s.c_str(), len, MasterItems[index].destination);
MasterItems[index].destination[len] = L'\0';
*/

#elif defined(WIDECHAR_IS_CHAR16) // WideChar = char16_t, wchar_t = 4 bytes

UCS4String s = UnicodeStringToUCS4String(Edit1->Text);
size_t len = std::min(s.Length-1, std::size(MasterItems[index].destination)-1); // UCS4String::Length includes the null terminator!
std::copy_n(&s[0], len, MasterItems[index].destination);
MasterItems[index].destination[len] = L'\0';

#else

// unsupported wchar_t size!

#endif

否则,如果要确保数组在所有平台上始终为16位UTF-16,则需要在数组中使用char16_tWideChar而不是wchar_tu前缀创建char16_t-based文字,RTL的_D()宏创建WideChar-based文字(根据平台使用Lu ),例如:

代码语言:javascript
复制
typedef struct recordstruct
{
    bool shop;
    bool bought;
    char16_t description[80]; // or: System::WideChar
} recordtype;

recordtype MasterItems[MAXITEMS]=
{
    false,false,u"Apples", // or: _D("Apples")
    false,false,u"Apricots", // or: _D("Apricots")
    false,false,u"Avocado", // or: _D("Avocado")
    ...
};

...

StrLCopy(MasterItems[index].description, Edit1->Text.c_str(), std::size(MasterItems[index].description)-1); // -1 for null terminator

/* or:
UnicodeString s = Edit1->Text;
size_t len = std::min(s.Length(), std::size(MasterItems[index].description)-1); // -1 for null terminator
std::copy_n(s.c_str(), len, MasterItems[index].description);
MasterItems[index].description[len] = u'\0'; // or: _D('\0')
*/
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62178127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档