F
Flzw
How to convert a std::string to a WCHAR* ?
is there any methods or something ? I can't find. Thanks
is there any methods or something ? I can't find. Thanks
Flzw said:How to convert a std::string to a WCHAR* ?
is there any methods or something ? I can't find. Thanks
How to convert a std::string to a WCHAR* ?
is there any methods or something ? I can't find. Thanks
How to convert a std::string to a WCHAR* ?
is there any methods or something ? I can't find. Thanks
Flzw said:How to convert a std::string to a WCHAR* ?
is there any methods or something ? I can't find. Thanks
Flzw said:Well, I tried to use
typedef std::basic_string<WCHAR> wstring;
and then I would use c_str() to get WCHAR*
the program compiles fine but it crashes on
wstring wstr = L"Test";
it seems to trigger a bad alloc and crash somewhere in an internal strlen
call
I know I can convert a basic std:string by getting the c_str() and then
copying it to a ushort array with std:copy
Huh?
but as it is an often called
function, I'd rather want it to be fast, using WCHAR to build strings would
be great.
Flzw said:Thanks, I decided to use wstring everywhere so I don't have to bother
with conversions (because I would need a lot of them and it would
affect efficiency)
the problem I have with wstring is when I initialise it for example
wstring test = L"Test";
Compiles, but crashes, seems to trigger a bad alloc and crash in some
internal strlen call
any idea on this ?
wchar_t *p=new wchar_t[s.size()];
Bob said:wchar_t *p=new wchar_t[s.size()];
I'm glad I'm not the only one that does this sometimes <g>
...should be:
wchar_t *p=new wchar_t[s.size()+1];
Bob said:wchar_t *p=new wchar_t[s.size()];
I'm glad I'm not the only one that does this sometimes <g>
...should be:
wchar_t *p=new wchar_t[s.size()+1];
Yes that could be reasonable for char *, but why for wchar_t*?
MSD said:I am using the following code for converting the std::wstring to
std::string.
#include <cstdlib>
#include <string>
.....
std::string strName;
std::wstring wstrName (L"ABCDEF");
if (wstrName.size() > 0)
{
int len = wcslen(wstrName.c_str());
int c = wcstombs( &strName[0], wstrName.c_str(), 1000);
cout << "len = " << len << "\ncharacters converted = " <<
c << "\n W - Name = " << wstrName << endl;
}
char *ptrName = strName.c_str();
.....
This is compiling fine, but as the length of the wstrName increases to
more than 8/9, it gives me Segmentation Fault and crashes.
I suspect some memory troubles, but do not have any solution for this. Do
you have any idea how to solve this?
It worked for me when I used the following:
char *s = (char *) malloc (len * sizeof(char));
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.