Solved

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

Posted on 2013-11-28
4
2,006 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
Template syntax for variable length arrays 9 71
Unresolved External Symbols 3 70
Error creating a new C++ project in ,net 20 21
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

864 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

19 Experts available now in Live!

Get 1:1 Help Now