Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-11-28
4
Medium Priority
?
2,941 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 1336 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 9

Assisted Solution

by:Murugesan Nagarajan
Murugesan Nagarajan earned 664 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 35

Accepted Solution

by:
sarabande earned 1336 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

618 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