how to assign or convert std::string to wstring?

From the previous question

if tran.AccountNumber is wchar_t *, how should I assigned *beg to it?
where beg is boost::tokenizer<escaped_list_separator<char> >::iterator.
Who is Participating?
sarabandeConnect With a Mentor Commented:
while defining a variable of any type it needs to be initialized to some default value
class variables were initialized in the constructor of the class which was called automatically. for std::wstring an empty string is the default. therefore, a default value is only required and sensible if it is different to empty string.

sarabandeConnect With a Mentor Commented:
you would use the function mbstowcs

std::string str =  *beg;       
std::wstring wstr;
wstr.resize(str.length(), L'\0');
mbstowcs(&wstr[0], str.c_str(), str.size());

Open in new window

Murugesan NagarajanConnect With a Mentor Subject-matter expert at C++ C delivery, implementation, and automation at UNIX oriented operating systems (Windows: CYGWIN_NT MINGW32_NT MINGW64_NT)Commented:
I agree with proposed statement from sarabande.
Also while defining a variable of any type it needs to be initialized to some default value.
std::wstring wstr( L"");

Open in new window

Function mbstowcs is present in the following file:
at Linux /usr/include/stdlib.h:
extern size_t mbstowcs (wchar_t *__restrict  __pwcs, __const char *__restrict __s, size_t __n) __THROW;

Open in new window

at Cygwin in windows in /usr/include/stdlib.h
size_t	_EXFUN(mbstowcs,(wchar_t *, const char *, size_t));

Open in new window

tommym121Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.