How to convert string to wstring by means of STL and C++ headers only?
-
Yeah once again about it! I know, there is a way to do it using crt function mbstowcs. The first thing I do not like about it I have single-byte character string (in the cp1251 encoding for example). The second it's not about stl.
-
Yeah once again about it! I know, there is a way to do it using crt function mbstowcs. The first thing I do not like about it I have single-byte character string (in the cp1251 encoding for example). The second it's not about stl.
-
-
like this: wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); return wstring(pw); } -- modified at 14:03 Wednesday 12th April, 2006
Look s about right - proof of the pudding's in the testing, of course!
-
Look s about right - proof of the pudding's in the testing, of course!
OMG! It's such a shame I did new without delete ;) GC is deep in my mind! wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); wstring ws(pw); delete [] pw; return ws; }
-
like this: wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); return wstring(pw); } -- modified at 14:03 Wednesday 12th April, 2006
You're not freeing the memory used by pw, unless I've missed something?
The Rob Blog
Google Talk: robert.caldecott -
OMG! It's such a shame I did new without delete ;) GC is deep in my mind! wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); wstring ws(pw); delete [] pw; return ws; }
You could always use a vector instead. :)
vector<wchar_t> pw(l);
...
use_facet<ctype<wchar_t> >(loc).widen(pc,pc+l-1,&pw[0]);
wstring ws(&pw[0]);
The Rob Blog
Google Talk: robert.caldecott -
OMG! It's such a shame I did new without delete ;) GC is deep in my mind! wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); wstring ws(pw); delete [] pw; return ws; }
As Robert says, you could use
std::vector
- or you could useboost::scoped_array<wchar_t>
[^] instead ofwchar_t
-
You're not freeing the memory used by pw, unless I've missed something?
The Rob Blog
Google Talk: robert.caldecottlocale global_locale; wstring s2w(const string &s,const locale &loc=global_locale) { size_t l=s.length()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; use_facet>(loc).widen(pc,pc+l-1,pw); pw[l-1]=L'\0'; wstring ws(pw); delete [] pw; return ws; }
-
locale global_locale; wstring s2w(const string &s,const locale &loc=global_locale) { size_t l=s.length()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; use_facet>(loc).widen(pc,pc+l-1,pw); pw[l-1]=L'\0'; wstring ws(pw); delete [] pw; return ws; }
Not exception safe ;) As others suggested, use std::vector or boost::scoped_array instead of new[]-delete[]
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Not exception safe ;) As others suggested, use std::vector or boost::scoped_array instead of new[]-delete[]
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
Ok, here comes cute solution :) wstring s2w(const string &s,const locale &loc=global_locale) { size_t l=s.length(); vector pw(l+1); use_facet>(loc).widen(&s[0],&s[l],&pw[0]); wstring ws(&pw[0]); return ws; } widen does not append trailing L'\0'! The question is: does vector always initialize values to 0?
-
Ok, here comes cute solution :) wstring s2w(const string &s,const locale &loc=global_locale) { size_t l=s.length(); vector pw(l+1); use_facet>(loc).widen(&s[0],&s[l],&pw[0]); wstring ws(&pw[0]); return ws; } widen does not append trailing L'\0'! The question is: does vector always initialize values to 0?
alabax wrote:
The question is: does vector always initialize values to 0?
Yes, the default value of a simple type using this vector ctor:
explicit vector(size_type count);
is 0. For classes, the default ctor is run, if no explicit value is provided in this vector ctor:
vector(size_type count, const T& value);
. -- The Blog: Bits and Pieces