Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: converting std::string to UnicodeString


This question is answered.


Permlink Replies: 4 - Last Post: Oct 27, 2014 10:18 AM Last Post By: Remy Lebeau (Te...
Kenneth Czepelka

Posts: 22
Registered: 1/3/06
converting std::string to UnicodeString  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 26, 2014 12:46 AM
I have a function that returns std::string and need to convert that to a Unicode string. I tried:

String S=(UnicodeString(getname());

but I get an error that says "could not find a match for UnicodeString::UnicodeString(std::string)"

I have some sample code that supposedly works that declares a variable and and sets it to the function return value like this:

std::string name;
name=getname();

if that helps any.
thanks.
Vladimir Zhuchko

Posts: 116
Registered: 1/22/98
Re: converting std::string to UnicodeString  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 26, 2014 2:27 AM   in response to: Kenneth Czepelka in response to: Kenneth Czepelka
Kenneth Czepelka wrote:
I have a function that returns std::string and need to convert that to a Unicode string. I tried:

String S=(UnicodeString(getname());

but I get an error that says "could not find a match for UnicodeString::UnicodeString(std::string)"

I have some sample code that supposedly works that declares a variable and and sets it to the function return value like this:

std::string name;
name=getname();

if that helps any.
thanks.

std::string str;

std::wstring wstr;

wstr.assign(str.begin(), str.end());

--
With best regards
Vladimir Zhuchko

Harry Bego

Posts: 22
Registered: 1/27/09
Re: converting std::string to UnicodeString
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 26, 2014 2:46 AM   in response to: Kenneth Czepelka in response to: Kenneth Czepelka
This will do it:

String S = getname().c_str();
Kenneth Czepelka

Posts: 22
Registered: 1/3/06
Re: converting std::string to UnicodeString  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 26, 2014 7:21 AM   in response to: Harry Bego in response to: Harry Bego
Harry Bego wrote:
This will do it:

String S = getname().c_str();

Thank you both for your replies. I used this method and it works fine.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: converting std::string to UnicodeString  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 27, 2014 10:18 AM   in response to: Kenneth Czepelka in response to: Kenneth Czepelka
Kenneth wrote:

I have a function that returns std::string and need to convert that
to a Unicode string. I tried:

String S=(UnicodeString(getname());

but I get an error that says "could not find a match for
UnicodeString::UnicodeString(std::string)"

Use this instead:

String S = getname().c_str();


Or this, which is slightly faster since the constructor won't have to hunt
for the length of the source string:

std::string tmp = getname();
String S(tmp.c_str(), tmp.length());


Do keep in mind that in both cases, the assignment will convert the std::string
data from Ansi to UTF-16 using the OS default Ansi charset. If you need
to convert using a specific charset, you can do one of these instead:

std::string tmp = getname();
String S = AnsiStringT<DesiredCodepageHere>(tmp.c_str(), tmp.length());


Or:

String S;
std::string tmp = getname();
if (!tmp.empty())
{
    int len = UnicodeFromLocaleChars(DesiredCodepageHere, 0, tmp.c_str(), 
tmp.length(), NULL, 0);
    if (len > 0)
    {
        S.SetLength(len);
        UnicodeFromLocaleChars(DesiredCodepageHere, 0, tmp.c_str(), tmp.length(), 
S.c_str(), len);
    }
}


Or:

String S;
std::string tmp = getname();
if (!tmp.empty())
{
    TBytes buf;
    buf.Length = tmp.length();
    memcpy(&buf[0], tmp.c_str(), buf.Length);
    std::auto_ptr<TEncoding> enc(TEncoding::GetEncoding(DesiredCodepageHere));
    // or: std::auto_ptr<TEncoding> enc(TEncoding::GetEncoding("DesiredCharsetHere"));
    S = enc->GetString(buf);
}


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02