Page 1 of 1
Converting std::string to wchar_t* and reverse
Posted: Fri Feb 19, 2010 8:22 pm
by Memorial76
std::string to wchar_t*:
Code: Select all
const wchar_t* CToolBox::strToWchart(std::string sInput)
{
wchar_t* wCharOutput = new wchar_t[1023];
size_t* sizeOut = new size_t;
size_t sizeInWords = 256;
const char* cStr;
cStr = sInput.c_str();
mbstowcs_s( sizeOut, wCharOutput, sizeInWords, cStr, strlen(cStr)+1);
return wCharOutput;
}
wchar_t* to std::string:
Code: Select all
const std::string CToolBox::wchartToStr(const wchar_t* wInput)
{
std::string sOutput = "";
size_t* nbOfChar = new size_t;
char* cOut = new char[1023];
size_t sizeInBytes = 1023;
wcstombs_s( nbOfChar, cOut, sizeInBytes, wInput, 1023);
sOutput += cOut;
return sOutput;
}
Posted: Fri Feb 19, 2010 9:59 pm
by Bate
or you simply use this when you need a wchar_t*
Code: Select all
stringw( std_string.c_str() ).c_str()
Posted: Fri Mar 05, 2010 5:00 pm
by Memorial76
thanks
Posted: Fri Mar 05, 2010 7:05 pm
by slavik262
std::string is actually a typedef of std::basic_string<char>, so you could use std::basic_string<wchar_t> if you'd like.
Posted: Sat Mar 06, 2010 6:02 pm
by Memorial76
slavik, it seems really interesting, could you be more precise on the way to do it please?
Bate, I can't find any "stringw" event in the C++ help...
Posted: Sat Mar 06, 2010 6:41 pm
by Lonesome Ducky
Stringw is exclusive to irrlicht, are you not using it? It seems an odd place to put code without using irrlicht

Posted: Sun Mar 07, 2010 7:27 am
by Memorial76
Lonesome Ducky wrote:Stringw is exclusive to irrlicht, are you not using it? It seems an odd place to put code without using irrlicht

Irrwhat?...
kidding
The fact is that I have some classes I don't want to have anything to do with Irrlicht...
Posted: Sun Mar 07, 2010 9:02 am
by Memorial76
This is an improved version (memory leaks removed):
Code: Select all
CConverter::CConverter()
{
m_wCharBuffer = new wchar_t[1023];
}
CConverter::~CConverter()
{
delete[] m_wCharBuffer;
}
const wchar_t* CConverter::strToWchart(std::string sInput)
{
size_t* sizeOut = new size_t;
size_t sizeInWords = 256;
const char* cStr;
cStr = sInput.c_str();
mbstowcs_s( sizeOut, m_wCharBuffer, sizeInWords, cStr, strlen(cStr)+1);
delete sizeOut;
return m_wCharBuffer;
}
const std::string CConverter::wchartToStr(const wchar_t* wInput)
{
std::string sOutput = "";
size_t* nbOfChar = new size_t;
char* cOut = new char[1023];
size_t sizeInBytes = 1023;
wcstombs_s( nbOfChar, cOut, sizeInBytes, wInput, 1023);
sOutput += cOut;
delete nbOfChar;
delete[] cOut;
return sOutput;
}
Posted: Mon Apr 12, 2010 11:04 am
by teto
Another great solution (a simple header):
http://utfcpp.sourceforge.net/
Posted: Mon Apr 12, 2010 12:50 pm
by Memorial76
Interesting, thanks

Posted: Wed May 26, 2010 6:35 pm
by fabietto
I've slightly modified the above class in order to make it working on my Mac (where I'm missing mbstowcs_s and wcstombs_s, so I'm forced to go for a more safe solution). If you might be interested in it, here we are.
Header:
Code: Select all
#ifndef CCONVERTER_H
#define CCONVERTER_H
#include <string>
class CConverter {
public:
CConverter();
~CConverter();
const wchar_t* strToWchart(std::string sInput);
const std::string wchartToStr(const wchar_t* wInput);
wchar_t *m_wCharBuffer;
};
#endif
Implementation:
Code: Select all
#include "cconverter.h"
CConverter::CConverter() {
m_wCharBuffer = new wchar_t[1023];
}
CConverter::~CConverter() {
delete[] m_wCharBuffer;
}
const wchar_t* CConverter::strToWchart(std::string sInput) {
size_t* sizeOut = new size_t;
size_t sizeInWords = 256;
const char* cStr;
cStr = sInput.c_str();
//mbstowcs_s( sizeOut, m_wCharBuffer, sizeInWords, cStr, strlen(cStr)+1);
mbstowcs(m_wCharBuffer, cStr, sizeInWords);
delete sizeOut;
return m_wCharBuffer;
}
const std::string CConverter::wchartToStr(const wchar_t* wInput) {
std::string sOutput = "";
size_t* nbOfChar = new size_t;
char* cOut = new char[1023];
size_t sizeInBytes = 1023;
//wcstombs_s( nbOfChar, cOut, sizeInBytes, wInput, 1023);
wcstombs(cOut,wInput,sizeInBytes);
sOutput += cOut;
delete nbOfChar;
delete[] cOut;
return sOutput;
}
Example of usage (very basic):
Code: Select all
#include "cconverter.h"
std::string a;
CConverter *stringConverter = new CConverter();
a = stringConverter->wchartToStr(simGUI->resultsDirectoryEditBox->getText());
Posted: Thu May 27, 2010 9:58 am
by Memorial76
thanks for your interest

Posted: Thu May 27, 2010 6:34 pm
by teto
you can also use
http://utfcpp.sourceforge.net/ which is a simple header doing this in a safe and easy way through iterators.
Typically all you need to do is:
utf8::utf8to16(name.begin(),name.end(), std::back_inserter(wname) );
Re: Converting std::string to wchar_t* and reverse
Posted: Mon Jul 04, 2011 6:53 pm
by agnas
Each time I need to convert from string/char * to wchar_t* I come to this thread, and each time I realized that the easy way to do it is something like this:
Code: Select all
string mystring="a string to be converted to wchar_t";
element->addItem (((wchar_t*)stringw(mystring).c_str()));
if you are in this forum you are using irrlicht so the irrlicht library is available to your application.
Re: Converting std::string to wchar_t* and reverse
Posted: Wed Jul 20, 2011 4:33 pm
by Memorial76
Interesting, I'm gonna try this asap