Page 1 of 1

Problems with a string

Posted: Sun Jan 25, 2004 3:24 am
by IrrLicht Newbie
Hi.

A few days ago I downloaded Irrlicht and successfully compiled it in VC 6.0 While I am still discovering Irrlicht in general, I have already begun to program a 3D game editor with Irrlicht, a tool that would save me resources and time, and basically beats the need to compile everything all the time. Sadly I mostly work on PERL projects (CGI, etc) elsewhere, and my C++ skills are not as sharp as they should be.

Allright, here goes the question. I have this piece of code:

----------------------------------

wchar_t svdScreenMessage[1024];

char abc[1024] = "TEXT MESSAGE 5";

// Some other stuff ...


swprintf(svdScreenMessage, 1024, L"%s", abc);

guienv->addStaticText(svdScreenMessage, true, rect<int>(10,50,200,70));


---------------------------

All right. Basically what I am trying to do here is to copy the abc string (declared with char) into svdScreenMessage (declared with wchar_t as provided by IrrLicht). It turns out that it actualy compiles, with no errors. However, when I run the program,instead of seeing "TEXT MESSAGE 5" I just see some odd symbols like |||||

The only reason I want to do this is because I wanted to create a dinamic text box that would inform me of things during the game, and thus I wanted to use functions such as strcpy and strcat, etc, which wchar_t wont accept.

Is there any way to copy a normal string declared with char to a string declared with wchar_t ????


Thanks.

-IrrLicht Newbie

Posted: Sun Jan 25, 2004 6:09 am
by tstuefe
Hi,

Yeah, you try to copy a singlebyte string into a Unicode string. Compiles fine, because swprintf simply takes the next pointer on the stack, whatever that is, and, seeing %s, tries to interpret it as a pointer to a zero-terminated Unicode string.

Simple solution 1:
use mbtowc() to convert your abc string to Unicode before using it.

Even simpler solution 2:
you could declare your abc string tobe unicode right away:
wchar_t abc [] = L"TEXT MESSAGE";


Question to other people: I'm pretty sure that gcc catches those errors, because I have seen it parsing the printf format string when compiling and warning me about wrong arguments. Does anyone know whether VC++ can do the same?

...thomas

Posted: Sun Jan 25, 2004 6:10 am
by keless
swprintf() expects you to send it wchar_t for "%S" thats why you got "|||"

you're going to need to convert your char to wchar_t before sending to swprintf()

there is a function, whos name I forget, something like "wstochar" and "chartows" which does conversion between the two.

Posted: Sun Jan 25, 2004 6:14 am
by rt
check out this thread http://irrlicht.sourceforge.net/phpBB2/ ... .php?t=906... i love my dout's :P

Posted: Sun Jan 25, 2004 12:26 pm
by mimi
Niko has a nice string class which does char wchar_t conversion and vs.
check out irr::core::string

Posted: Sun Jan 25, 2004 8:28 pm
by Guest
Thanks people. With all you have told me, I think I will be able to find my way now towards solving this problem. Thanks for the guidelines/hints, all of you :)