Problems with a string

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
IrrLicht Newbie

Problems with a string

Post 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
tstuefe
Posts: 40
Joined: Wed Jan 07, 2004 12:53 pm
Location: Heidelberg, Germany
Contact:

Post 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
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post 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.
a screen cap is worth 0x100000 DWORDS
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Post by rt »

check out this thread http://irrlicht.sourceforge.net/phpBB2/ ... .php?t=906... i love my dout's :P
mimi

Post by mimi »

Niko has a nice string class which does char wchar_t conversion and vs.
check out irr::core::string
Guest

Post 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 :)
Post Reply