【发布时间】:2014-11-08 11:43:25
【问题描述】:
我的配置:
- 编译器:gnu gcc 4.8.2
- 我用 C++11 编译
- 平台/操作系统:Linux 64 位 Ubuntu 14.04.1 LTS
我想用 wchar_t* 提供一个方法,并在许多需要 XMLCh* 的 xecerces 库方法中使用它,但我不知道如何从一个转换到另一个。如果您使用 char* 而不是 wchar_t* 很容易,但我需要使用宽字符。在 Windows 下,我可以轻松地从一个转换到另一个,但它在我的 linux 机器上不起作用。不知何故,我必须手动将 wchar_t* 转换为 XMLCh*
我通过库 libxerces-c-3.1.so 链接,它专门使用 XMLCh*。 XMLCh 可以处理宽字符,但我不知道如何将它提供给它,以及如何从 XMLCh* 返回 wchar_t*
我开发了这个,但它不起作用(这里我吐出了一个 wstring,它在清理内存方面比指针更容易管理:
static inline std::wstring XMLCh2W(const XMLCh* tagname)
{
std::wstring wstr;
XMLSize_t len1 = XMLString::stringLen(tagname);
XMLSize_t outLen = len1 * 4;
XMLByte ut8[outLen+1];
XMLSize_t charsEaten = 0;
XMLTransService::Codes failReason; //Ok | UnsupportedEncoding | InternalFailure | SupportFilesNotFound
XMLTranscoder* transcoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor("UTF-8", failReason,16*1024);
unsigned int utf8Len = transcoder->transcodeTo(tagname,len1,ut8,outLen,charsEaten,XMLTranscoder::UnRep_Throw);// XMLTranscoder::UnRep_Throw UnRep_RepChar
ut8[utf8Len] = 0;
std::wstring wstr = std::wstring((wchar_t*)ut8);//I'm not sure this is actually ok to do
return wstr;
}
【问题讨论】:
-
stackoverflow.com/questions/25782247/… 的副本。并不是说你在这里得到的答案可能比我得到的更多。