Solved

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

Posted on 2013-11-28
4
2,062 Views
Last Modified: 2013-12-07
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.
0
Comment
Question by:tommym121
  • 2
4 Comments
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 334 total points
ID: 39684814
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


Sara
0
 
LVL 7

Assisted Solution

by:Murugesan Nagarajan
Murugesan Nagarajan earned 166 total points
ID: 39697892
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

0
 
LVL 33

Accepted Solution

by:
sarabande earned 334 total points
ID: 39697935
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.

Sara
0
 

Author Closing Comment

by:tommym121
ID: 39703280
thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question