我正在使用C++Builder 10.3力拓为安卓开发一个多平台应用程序。
我有如下一系列数据:
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()等。
请问我怎么做?
发布于 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上一样):
wchar_t和char16_t都是双字节字符.macOS、iOS、和Android上,wchar_t是一个4字节的字符.因此,要声明UTF-16常量字符串,在Windows上使用L或u前缀,而在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宏编写相应以L或u作为前缀的常量字符串。示例:
UnicodeString(_D("Text"))
为了确保在所有平台上使用UTF-16,System::WideChar类型是wchar_t在Windows上的别名,在其他平台上是char16_t的别名。UnicodeString是WideChar元素的容器。
因此,如果对数组使用wchar_t,那么在非Windows平台上,首先需要在运行时将UnicodeString转换为UTF-32,例如使用RTL的UnicodeStringToUCS4String()函数,然后才能将数据复制到数组中,例如:
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_t或WideChar而不是wchar_t。u前缀创建char16_t-based文字,RTL的_D()宏创建WideChar-based文字(根据平台使用L或u ),例如:
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')
*/https://stackoverflow.com/questions/62178127
复制相似问题