How to convert wstring to wchar_t*? C++

有些话、适合烂在心里 提交于 2019-12-10 12:34:50

问题


I would like to convert wstring to wchar_t*. I have tried everything what i know, please help. I would like to convert wstring to wchar_t*.


回答1:


Did you try reading the reference

const wchar_t* wcs = s.c_str();



回答2:


There is no way to convert wstring to wchar_t* but you can convert it to const wchar_t* which is what answer by K.Kirsz says.

This is by design because you can access a const pointer but you shouldn't manipulate the pointer. See a related question and its answers.

The best bet is to create a new string using _wcsdup and access the non const buffer, an ascii example is given there.

For unicode:

    wstring str = L"I am a unicode string";
    wchar_t* ptr = _wcsdup(str.c_str());
    // use ptr here....
    free(ptr); // free memory when done


来源:https://stackoverflow.com/questions/44985451/how-to-convert-wstring-to-wchar-t-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!