[英]How to convert wchar_t[] to basic_string<_Elem>?
I need to convert a project from VS2003 to VS2008.我需要将项目从 VS2003 转换到 VS2008。 In the following code:在以下代码中:
wchar_t wpom[30];
mbtowc(wpom, "olaboga", 10);
ati_dom::DOMString w = wpom;
I get an error (on the last line): Cannot convert from 'wchar_t[30]' to 'basic_string<_Elem>').我收到一个错误(在最后一行):无法从 'wchar_t[30]' 转换为 'basic_string<_Elem>')。
I tried to modify it to:我试图将其修改为:
wchar_t wpom[30];
mbtowc(wpom, "olaboga", 10);
std::basic_string<wchar_t> basic_wpom(wpom);
ati_dom::DOMString w = basic_wpom;
But all I acomplished is to get another error: Cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::basic_string<_Elem>'但我只完成了另一个错误:无法从 'std::basic_string<_Elem,_Traits,_Ax>' 转换为 'std::basic_string<_Elem>'
How can I convert wchar_t[] to basic_string<_Elem> and not to basic_string<_Elem,_Traits,_Ax>...?如何将 wchar_t[] 转换为 basic_string<_Elem> 而不是 basic_string<_Elem,_Traits,_Ax>...?
Just use a std::wstring directly via the constructor that takes a pointer to the first element and the length of the array:只需通过构造函数直接使用 std::wstring 即可,该构造函数采用指向第一个元素的指针和数组的长度:
wchar_t warr[ 30 ];
// populate the array
std::wstring wstrTemp( &warr[ 0 ], 30 );
Sorry for not responding - i found out what the problem was on my own.抱歉没有回复 - 我自己发现了问题所在。
As it turns out, DOMString is declared like this:事实证明,DOMString 是这样声明的:
typedef std :: basic_string< unsigned short > DOMString;
So simple casting to unsigned short did the trick:如此简单地转换为 unsigned short 就可以了:
ati_dom::DOMString w = (unsigned short *)wpom;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.