"Flzw" <fl0wnz@wanadoo .fr> wrote in message
news:cgs8lo$a9q $1@news-reader3.wanadoo .fr...[color=blue]
> How to convert a std::string to a WCHAR* ?
>
> is there any methods or something ? I can't find. Thanks
>[/color]
Well, I tried to use
typedef std::basic_stri ng<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.
Le dimanche 29 août 2004 à 11:46:47, Flzw a écrit dans comp.lang.c++ :
[color=blue]
> How to convert a std::string to a WCHAR* ?
>
> is there any methods or something ? I can't find. Thanks[/color]
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() );
// real call
::mbstowcs(&out put[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
On Sun, 29 Aug 2004 11:46:47 +0200, "Flzw" <fl0wnz@wanadoo .fr> wrote:
[color=blue]
>How to convert a std::string to a WCHAR* ?
>is there any methods or something ? I can't find. Thanks[/color]
<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:
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.
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
Flzw wrote:
[color=blue]
> Well, I tried to use
> typedef std::basic_stri ng<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[/color]
wstring is an existing C++ string type defined in <string>. You can use
it directly.
[color=blue]
> 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[/color]
Huh?
[color=blue]
> but as it is an often called
> function, I'd rather want it to be fast, using WCHAR to build strings would
> be great.[/color]
Flzw wrote:
[color=blue][color=green]
>> std::wstring output;[/color]
>
> 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 ?[/color]
Either you're doing something else that corrupts memory before or your
implementation has a bug. The following program works here:
Bob Hairgrove wrote:[color=blue]
> On Sun, 29 Aug 2004 15:04:20 +0300, Ioannis Vranos
> <ivr@guesswh.at .grad.com> wrote:
>
>[color=green]
>> wchar_t *p=new wchar_t[s.size()];[/color]
>
>
> 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];[/color]
Yes that could be reasonable for char *, but why for wchar_t*?
On Mon, 30 Aug 2004 01:12:11 +0300, Ioannis Vranos
<ivr@guesswh.at .grad.com> wrote:
[color=blue]
>Bob Hairgrove wrote:[color=green]
>> On Sun, 29 Aug 2004 15:04:20 +0300, Ioannis Vranos
>> <ivr@guesswh.at .grad.com> wrote:
>>
>>[color=darkred]
>>> wchar_t *p=new wchar_t[s.size()];[/color]
>>
>>
>> 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];[/color]
>
>
>
>Yes that could be reasonable for char *, but why for wchar_t*?[/color]
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.
MSD wrote:
[color=blue]
> 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 << "\ncharacte rs 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.[/color]
[color=blue]
>
> I suspect some memory troubles, but do not have any solution for this. Do
> you have any idea how to solve this?[/color]
Well, you write up to 1000 characters to the contents of strName without
caring for its size.
Comment