将ICU Unicode字符串转换为std :: wstring(或wchar_t *)

问题描述 投票:0回答:1

是否有从icu std::wstring创建UnicodeString的icu函数?我一直在搜索ICU手册,但找不到。

((我知道我可以将UnicodeString转换为UTF8,然后转换为依赖于平台的wchar_t*,但我正在UnicodeString中寻找一个可以进行此转换的功能。

c++ icu
1个回答
0
投票

C ++标准没有规定std::wstring的任何特定编码。在Windows系统上,wchar_t是16位,在Linux,macOS和其他几个平台上,wchar_t是32位。就C ++的std::wstring而言,它只是wchar_t的任意序列,几乎与std::string只是char的任意序列一样。

icu::UnicodeString似乎没有内置的创建std::wstring的方法,但是如果您确实要创建std::wstring,则可以使用基于C的API u_strToWCS(),如下所示:

u_strToWCS()

[据推测,icu::UnicodeString ustr = /* get from somewhere */; std::wstring wstr; int32_t requiredSize; UErrorCode error = U_ZERO_ERROR; // obtain the size of string we need u_strToWCS(nullptr, 0, &requiredSize, ustr.getBuffer(), ustr.length(), &error); // resize accordingly (this will not include any terminating null character, but it also doesn't need to either) wstr.resize(requiredSize); // copy the UnicodeString buffer to the std::wstring. u_strToWCS(wstr.data(), wstr.size(), nullptr, ustr.getBuffer(), ustr.length(), &error); 将使用最有效的方法从u_strToWCS()转换为UChar(如果它们的大小相同,那么我想它只是简单的副本)。

© www.soinside.com 2019 - 2024. All rights reserved.