Converting std::string to wchar_t* and reverse

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Converting std::string to wchar_t* and reverse

Post 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;

}
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

or you simply use this when you need a wchar_t* :)

Code: Select all

stringw( std_string.c_str() ).c_str()
Never take advice from someone who likes to give advice, so take my advice and don't take it.
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

thanks
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post 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.
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post 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...
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Stringw is exclusive to irrlicht, are you not using it? It seems an odd place to put code without using irrlicht :wink:
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post 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 :wink:
Irrwhat?...
kidding :wink:

The fact is that I have some classes I don't want to have anything to do with Irrlicht...
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post 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;

}
teto
Posts: 159
Joined: Thu Dec 03, 2009 9:37 pm
Location: /home
Contact:

Post by teto »

Another great solution (a simple header):
http://utfcpp.sourceforge.net/
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

Interesting, thanks :wink:
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
fabietto
Posts: 93
Joined: Wed Sep 24, 2008 4:38 pm
Location: Plymouth, UK
Contact:

Post 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());
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Post by Memorial76 »

thanks for your interest :wink:
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
teto
Posts: 159
Joined: Thu Dec 03, 2009 9:37 pm
Location: /home
Contact:

Post 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) );
agnas
Posts: 20
Joined: Tue Mar 27, 2007 6:16 pm
Location: Venezuela
Contact:

Re: Converting std::string to wchar_t* and reverse

Post 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.
/\+++/\
< agnas >
\/+++\/
micronosis.com
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Re: Converting std::string to wchar_t* and reverse

Post by Memorial76 »

Interesting, I'm gonna try this asap
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
Post Reply