std::string -> WCHAR*

F

Flzw

How to convert a std::string to a WCHAR* ?

is there any methods or something ? I can't find. Thanks
 
F

Flzw

Flzw said:
How to convert a std::string to a WCHAR* ?

is there any methods or something ? I can't find. Thanks

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 but as it is an often called
function, I'd rather want it to be fast, using WCHAR to build strings would
be great.
 
B

Bob Hairgrove

How to convert a std::string to a WCHAR* ?
is there any methods or something ? I can't find. Thanks

<OT-rant>
WCHAR is a non-standard Windows type. Therefore, it is off-topic in
comp.lang.c++ which concerns itself only with ANSI and ISO standard
C++ language topics. Please read the FAQ for this newsgroup at:
http://www.parashift.com/c++-faq-lite/
</OT-rant>

Nevertheless, you are stuck with a std::string and need to know what
to do with it. It depends on how the std::string's character data is
encoded, i.e. which locale or code page is used.

Fr starters, I will assume you know that there are several different
Unicode encodings (http://www.unicode.org). WCHAR is defined as a
"16-bit Unicode character" which is only one of them.

For data which comes from Western European code set (i.e. ISO-8859-1
or ISO-8859-15), the conversion is trivial since the MSB (most
significant byte) is always 0. You need to supply a buffer of WCHAR,
which is large enough to contain all the character data plus one
terminating null WCHAR, and merely copy the characters from the
string. Don't use memcpy or such, but use a loop and copy it character
by character into the buffer. If the buffer is allocated dynamically,
be sure to release the memory by calling delete[] (if you used new[])
when you are done, or else use a smart pointer which can handle array
data (std::auto_ptr<> cannot ... there are such smart pointers in the
Boost library, though: http:://www.boost.org ).

Since your post seems to originate in France, be aware that the Euro
character "€" has a Unicode encoding of 0x20ac, therefore the MSB is
*not* zero for this particular character. In Windows, it is defined as
0x80 which is (AFAIK) a non-printable character in ISO-8859-1 code
page.

If there is a different code set involved, you will need to use either
the libraries supplied by the framework you are using for development
(e.g. MFC, VCL on Borland) or use the Windows API functions suitable
for this.

Your compiler may have implemented locale facets which facilitate some
conversions via std::[w]iostream, e.g. check out:

std::ios_base::imbue(const std::locale &)).

Note that some library functions for COM/OLE on Windows expect a BSTR
argument, or return a BSTR, which is a non-standard OLE data type
although I believe it is defined as WCHAR*. This is a horse of an
entirely different color, and there are issues with memory management
which are OS-specific. If this is what you need, go to one of the
Microsoft newsgroups which deals with Windows-specific C++
development.
 
S

Serge Paccalin

Le dimanche 29 août 2004 à 11:46:47, Flzw a écrit dans comp.lang.c++ :
How to convert a std::string to a WCHAR* ?

is there any methods or something ? I can't find. Thanks

The closest-to-standard way I can see is:

#include <cstdlib>
#include <string>
....

const std::string input = "string to convert";

// null-call to get the size
size_t needed = ::mbstowcs(NULL,&input[0],input.length());

// allocate
std::wstring output;
output.resize(needed);

// real call
::mbstowcs(&output[0],&input[0],input.length());

// You asked for a pointer
wchar_t *pout = output.c_str();

--
___________ 2004-08-29 12:47:10
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
F

Flzw

std::wstring output;

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 ?
 
I

Ioannis Vranos

Flzw said:
How to convert a std::string to a WCHAR* ?

is there any methods or something ? I can't find. Thanks


WCHAR is a system-dependent type, so I will use wchar_t for a portable
example:



#include <string>


int main()
{
using namespace std;

string s="This is a test";

wchar_t *p=new wchar_t[s.size()];


for(string::size_type i=0; i<s.size(); ++i)
p=s;
}






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
I

Ioannis Vranos

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


wstring is an existing C++ string type defined in <string>. You can use
it directly.

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.


You can.


#include <string>


int main()
{
using namespace std;

wstring s=L"This is a test";
}






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
R

Rolf Magnus

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 ?

Either you're doing something else that corrupts memory before or your
implementation has a bug. The following program works here:

#include <iostream>
#include <string>

int main()
{
std::wstring str = L"Hello, world";
std::wcout << str << std::endl;
}
 
B

Bob Hairgrove

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*?

Same as for char * ... to delimit the end of the array with a null
wchar_t, for example when initializing a std::wstring with a wchar_t*.

Of course, one could also use assign() with iterators.
 
M

MSD

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?

The max limit for wstrName can go upto 30 in my case.

Thanks
 
R

Rolf Magnus

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?

Well, you write up to 1000 characters to the contents of strName without
caring for its size.
 
M

MSD

Thnks Rolf.

It worked for me when I used the following:

char *s = (char *) malloc (len * sizeof(char));

and then using this s instead of &strName[0] in wcstombs function.

Thanks for your quick reply.

MSD
 
T

Tilman Kuepper

Hi,
It worked for me when I used the following:

char *s = (char *) malloc (len * sizeof(char));

You could also try something like...

#include <vector>
std::vector<char> buf(len);

....and use &buf[0] as argument to wcstombs. I think
this solution is more robust; e. g. you don't have to
remember to call free(s).

Tilman
 

Ask a Question

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.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top