Conversion from wchar_t to char

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
GavRo
Posts: 32
Joined: Fri Dec 29, 2006 12:15 am

Conversion from wchar_t to char

Post by GavRo »

I have another small problem.

For my game I'm using gamemonkey as an embedded scripting language so I don't have to hardcode everything in C++. Now in C++, i have a function called void CharacterSay(irr::core::stringw) that is bound to a script function called CharacterText(text).

Now the gamemonkey's engine returns text as char *, but Irrlicht works with wchar_t *. So when I say in my script file:

CharacterText("welcome to my game");

It sends that text to the bound function to display in a dialog box(like those found in Japanese RPGs).

Now when I run the program, due to improper conversion of the char* to wchar_t *, instead of my text, I get boxes instead of letters.

If I test the function hardcoded inside C++ I get my text, since in C++ I have to enter it as a wchar_t.

Please how do I convert from char* to wchar_t * properly so I get my letters and numbers.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Search the forum, many times asked and answered...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
datamagik
Posts: 26
Joined: Wed Feb 14, 2007 11:25 pm
Location: Bar Harbor, Maine USA

Post by datamagik »

After googling wchar_t, it is a datatype capable of storing all characters in all languages that might be supported. It seems to have been adapted to storing UNICODE representations of data as either 16 or 32 bit characters.

So long as all of your strings fall within the original ASCII 7 character set you can perform a simple conversion, copying the wide characters to traditional C characters. A C string is an array of characters terminating in a null character (a zero).

For any string w with a non-zero length this should work:

Code: Select all

void WCharToChar(char* c, wchar_t* w)
{
    int i=0;

    if (w[i])
        for (i=0; w[i]; i++)
            c[i] = (char)w[i];
 
    c[i] = 0;
}
This function will break if any of the characters are greater than 255 (which covers up to Latin-1 extensions as I recall), or if the string w is not a valid string (if w does not contain a null end of string this function will overwrite variables after the end of c...presumably you will have already checked to see that w contains a valid string before calling this function.

Let me know if this helps!

-Jay
Last edited by datamagik on Thu Feb 22, 2007 10:39 pm, edited 1 time in total.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Right, why doing it simple if there also is a hard way (like this function)... :roll:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
datamagik
Posts: 26
Joined: Wed Feb 14, 2007 11:25 pm
Location: Bar Harbor, Maine USA

Post by datamagik »

Although it was easier for me to just jot down that conversion function than comb through all the entries that come up in search :wink: I realized our friend must know of something in the engine which would be more reliable.

Sure enough, the conversion routine is already available to us and was described in this earlier posting which I found by searching for "wchar_t" :D

http://irrlicht.sourceforge.net/phpBB2/ ... ght=wchart
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Well, I didn't want this, but alright... ;)

You're reinventing the wheel !!!
Use this:

Code: Select all

// convert from char* to wchar_t* 
mbstowcs(wchar_t*, const char*, size_t);

// convert from wchar_t* to char* 
wcstombs(char*, const wchar_t*, size_t);
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
GavRo
Posts: 32
Joined: Fri Dec 29, 2006 12:15 am

Ohh...

Post by GavRo »

Haha thanks a lot.
Post Reply