C++ string, wstring conversion functions

Download here. string-wstring-conversion-functions

// Filename    : string_wstring.cpp
// Author      : A.G.Raja
// License     : GPL
// Website     : https://agraja.wordpress.com

#include <iostream>

using namespace std;

string wstring2string(wstring wstr) {
string str(wstr.length(),’ ‘);
copy(wstr.begin(),wstr.end(),str.begin());
return str;
}

wstring string2wstring(string str) {
wstring wstr(str.length(),L’ ‘);
copy(str.begin(),str.end(),wstr.begin());
return wstr;
}

int main() {
wstring wstr = string2wstring(“raja”);
string str = wstring2string(wstr);
cout<<str<<endl;
}

Download here. string-wstring-conversion-functions

Advertisements

4 thoughts on “C++ string, wstring conversion functions

  1. Pingback: C++ string, wstring conversion functions | Today's Bargain Electronics Store

  2. Pingback: Business blog » Blog Archive » C++ string, wstring conversion functions

  3. Hi, thats OK for words in english, but in spanish don’t. Try to convert: documentación, gané, or other word with á, é, í, ó or ú.

    Bye.

  4. Of course, you’ll abuse *your* privilege to refuse my comment, nevertheless I have rigth to convey the truth: this is just a crap in all aspects.

    Do meditate on the following –

    // this stuff is Public Domain 😀
    std::wstring string2wstring(std::string & str) {
    return std::wstring(str.begin(),str.end());
    }

    Though this does NO conversion (like yours) but at last with no overhead 😉

    Nothing personal, I’m able to code even worse, but taking care no one can watch it. Please do not confuse teaching and learning.

    WBR, Serge.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s