简体   繁体   English

如何将 wchar_t[] 转换为 basic_string<_Elem>?

[英]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.

相关问题 转换std :: basic_string <wchar_t> 和std :: basic_string <uint16_t> - Convert std::basic_string<wchar_t> and std::basic_string<uint16_t> 没有合适的构造函数来将“ const char [8]”转换为“ std :: basic_string” <wchar_t, std::char_traits<wchar_t> ,std :: allocator <wchar_t> &gt;” - no suitable constructor exists to convert from “const char [8]” to “std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>” 无法从“ TCHAR [260]”转换为“ std :: basic_string” <wchar_t,std::char_traits<wchar_t> ,性病::分配器 <wchar_t> &gt; - Cannot convert from 'TCHAR [260]' to 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> 将const wchar_t *转换为const std :: basic_string的更好方法<char_t> - Better way to convert const wchar_t * to const std::basic_string<char_t> boost :: filesystem :: path :: native()返回std :: basic_string <wchar_t> 而不是std :: basic_string <char> - boost::filesystem::path::native() returns std::basic_string<wchar_t> instead of std::basic_string<char> 如何将 std::string 转换为 wchar_t* - How to convert std::string to wchar_t* gdb PrettyPrinter插件例程StdStringPrinter在处理std :: basic_string时崩溃 <wchar_t(,.*)?> $ - gdb PrettyPrinter Plugin routine StdStringPrinter crashing when dealing with std::basic_string<wchar_t(,.*)?>$ 如何将wchar_t(或wchar_t *或CORBA :: WChar *)转换为字符串? - How to convert wchar_t (or wchar_t* or CORBA::WChar*) to string? 将 wchar_t 转换为字符串? - Convert wchar_t to string? 从&#39;std :: wstring {aka std :: basic_string <wchar_t> }&#39;转换为非标量类型&#39;UString {aka std :: basic_string <char> }&#39; - conversion from ‘std::wstring {aka std::basic_string<wchar_t>}’ to non-scalar type ‘UString {aka std::basic_string<char>}’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM