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());