Solved

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

Posted on 2013-11-28
4
1,904 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 32

Assisted Solution

by:sarabande
sarabande earned 334 total points
Comment Utility
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
Comment Utility
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 32

Accepted Solution

by:
sarabande earned 334 total points
Comment Utility
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
Comment Utility
thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now