string conversion!!!!! std::string to wchar_t ....HELP!

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
iNVERSE-sQUARE
Posts: 6
Joined: Wed Apr 07, 2004 8:48 am

string conversion!!!!! std::string to wchar_t ....HELP!

Post by iNVERSE-sQUARE »

Hi,

has anyone done this before?

convert std::string to const wchar_t

core::stringc doesn't seem to equate to std::string either, its a nightmare!

I have to do this for Irrlicht's draw->() function to work.

Thanks
Revelation:10,7
Guest

Post by Guest »

use the search function, this has been asked at least a million times :!:

bye!
iNVERSE-sQUARE
Posts: 6
Joined: Wed Apr 07, 2004 8:48 am

string conversion!!!!! std::string to wchar_t ....HELP!

Post by iNVERSE-sQUARE »

Hi, yeah I did do the search and a lot of 'similar' type results came up, although a few orders of magnitude less than a millian :wink: However my problem is specifically with std::string types and the conversion of this to wchar_t or wide chars. There was no match that I could find, and incidently this is a huge problem according to searches on the net as I have discovered over the past week - it's not trivial - and as a beginner with C++ it is hellishly hard to decifer other peoples solutions. Perhaps you know of an easy way. What I REALY want to do is load UNICODE text from a file into something like a vector template array, then send these to Irrlicht's

font->draw(const wchar_t*, position, color);

I tried various std:string functions with getline() from the Standard Library to read these strings but can't get wchars out of them - that would help. Thanks
Revelation:10,7
r3i
Posts: 147
Joined: Wed Jun 29, 2005 10:15 am
Location: Sorrento
Contact:

Post by r3i »

It can be useful? :D

stringw wide_char_string = selectedSceneNode->getName();
stringc stringaNormale = wide_char_string.c_str();

I've found it with search function ... lol!!
"We work in the dark, we do what we can, we give what we have, Our doubt is our passion and our passion is our task. The rest: is art of Madness" (H.James)
Guest

Post by Guest »

Code: Select all


#include <wchar.h>

// Don't forget, you must delete[] the returned pointer when finished with it
wchar_t *widestring(std::string s) 
{
	size_t size = s.size();
	wchar_t *text = new wchar_t[size+sizeof(wchar_t)]; //s.size() doesn't include NULL terminator
	const char *data = &s[0];
	mbsrtowcs(text, &data, size, NULL);
	text[size] = L'\0';

	return text;
}

...

wchar *foo = widestring(std::string("bar")); // foo = L"bar";
...
delete[] foo;

Guest

wchar_t conversion

Post by Guest »

Thanks, this is what I'm looking for, minor issue....

My DEV C++ says 'undefined reference to mbsrtowcs'

Is there another header apart from wchar .h needed. I've included all the
standard headers like <stream>, <stdio>, etc.

On the other hand, perhaps you might know how to set Dev for /Zc:wchar_t
setting which I understand is important for Unicode?

Thanks
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

it's easier to do:

std::string s;
t_wchar* w = stringw(s.c_str()).c_str();

font->draw(w,position, color); or font->draw(stringw(s.c_str()).c_str(),position, color);

converting back would be:

std::string s2;
s2 = stringc(w).c_str();
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
iNVERSE-sQUARE
Posts: 6
Joined: Wed Apr 07, 2004 8:48 am

strings

Post by iNVERSE-sQUARE »

Hi, thanks Sudi for the suggestion - I will try it now, and thanks 'guest' for the previous post which works too, though is more complex. Incidentaly, you need libmsvcp60.a in your library defs to work the mbsrtowcs(text, &data, size, NULL); function.

On another note which someone might know of: Using SDL streams for Unicode seems to require a compiler switch, /Zc or something before the compiler will recognize _wfopen or other wide-char functions, how does one do this from DEV's IDE? I'm trying to read Unicode file but can't get the wchar type equivelent functions to work from SDL. Thanks in advance... :o
Revelation:10,7
Post Reply