agraja.wordpress.com

C++ string, wstring conversion functions

In Cpp on September 8, 2008 at 6:15 am

Download here. string-wstring-conversion-functions

// Filename    : string_wstring.cpp
// Author      : A.G.Raja
// License     : GPL
// Website     : http://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

  1. [...] here: C++ string, wstring conversion functions char, communication, conversion, download, downloads, entertainment, images, linux, memory, [...]

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

    Bye.

  3. 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 :-D
    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.