C++ convert string type to UnicodeString type

Given a variable of type string how can the value of this string be assigned to a UnicodeString variable ?

eg.

string s = "Hello";

UnicodeString u;

u = s ; // yields an error

Using embarcadero C++ builder XE3
LVL 2
Roger AlcindorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Try

string s = "Hello";

UnicodeString u(s.c_str());

Open in new window


This is the constructor that takes an ANSI string (or better 'char*') and should do what you want to achieve. See also http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/System__UnicodeString__UnicodeString.html

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
>> Given a variable of type string how can the value of this string be assigned to a UnicodeString variable ?
There doesn't appear to be an assignment operator for a std::string but you can easily write one yourself (and borrow from the construction that jkr has show, above). Something like this...

// works for both std::string and std::wstring
template<typename T>
UnicodeString & operator = (UnicodeString & lhs, std::basic_string<T> const & rhs)
{
   return lhs = UnicodeString(rhs.c_str());
}

// this should now work
u = s;

Open in new window

sarabandeCommented:
to add to above solutions:

since UnicodeString has a constructor that takes a const char * as shown by jkr, you can do

UnicodeString u = "Hello";

or

std::string s = "Hello";
UnicodeString u = s.c_str();

note, the second is not an assignment but an initialization what means it is only alternative syntax to the statement jkr has posted.

Sara
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Roger AlcindorAuthor Commented:
evilrix's solution looks elegant but compiles with the following error :
[BCC32 Error] Unit1.h(16): E2239 '= <T>(UnicodeString &,const basic_string<T,char_traits<T>,allocator<T> > &)' must be a member function
sarabandeCommented:
the error is correct.

An assignment operator shall be implemented by a non-static member function with exactly one parameter (C++03 13.5.3/1).

that is different to operator== which either could be a member or a (static) non-member function.

Sara
Roger AlcindorAuthor Commented:
Thanks for your comments,

Roger
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.