char and wchar_t

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
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

char and wchar_t

Post by monkeycracks »

Code: Select all

struct EnumerationDataStruct
{
char Name[999];
};
Then later

Code: Select all

                    EnumerationDataStruct enumerationDataStruct;
                      IGUIEnvironment* guienv = device->getGUIEnvironment();
                      blah = enumerationDataStruct.Name;
                      guienv->,rect<int>(20,40,220,300),true,true,inftab);
75 C:\Documents and Settings\Zack\Desktop\dsf\gh\Game.h invalid conversion from `char*' to `wchar_t'

I can't seem to figure out how to solve this... Can someone post some code and not a 'Search the Forums noob' reply?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Use wchar_t instead of char?!
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Code: Select all

struct EnumerationDataStruct
{
wchar_t Name[999];
};

Code: Select all

                      EnumerationDataStruct enumerationDataStruct;
                      IGUIEnvironment* guienv = device->getGUIEnvironment();
                      wchar_t blah[999] = enumerationDataStruct.Name;
                      guienv->addStaticText(blah, rect<int>(20,40,220,300),true,true,inftab);
75 C:\Documents and Settings\Zack\Desktop\dsf\gh\Game.h invalid initializer

I've really got no clue what to do as of now
kompadre
Posts: 8
Joined: Tue Aug 08, 2006 9:56 pm
Contact:

Post by kompadre »

try this =)

Code: Select all

                      
EnumerationDataStruct enumerationDataStruct;
IGUIEnvironment* guienv = device->getGUIEnvironment();
wchar_t blah[999]; 
wcscpy(blah, enumerationDataStruct.Name);
guienv->addStaticText(blah, rect<int>(20,40,220,300),true,true,inftab);

or even this

Code: Select all

                      
EnumerationDataStruct enumerationDataStruct;
IGUIEnvironment* guienv = device->getGUIEnvironment();
guienv->addStaticText(enumerationDataStruct.Name, rect<int>(20,40,220,300),true,true,inftab);
P.S. Teh mistake trying to assign a value to a string array from another string array in C/C++ is quite common to programmers coming from scripting languages, you should learn some C++ (especially pointers and strings).
Last edited by kompadre on Sat Sep 02, 2006 1:09 am, edited 1 time in total.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Both compile without error, yet when it initializes and you look at the tab with the information on it.

it comes up with some strange characters that look somewhat like C's and a square little box like when a language isn't supported.

I know its not the transfer from the server to the client being corrupted because I have it set to say what is received in the console.

Any idea? I'll tinker with it until then.
kompadre
Posts: 8
Joined: Tue Aug 08, 2006 9:56 pm
Contact:

Post by kompadre »

You're receiving char and not wide char (wchar_t) from server I guess. The trick to convert one to anoter for you would be something like

Code: Select all

char one[128];
wchar_t two[128];
swprintf(two, 128, "%hs", one);
You should do this when actually reading the string from server (filling the Name property of your structure).
krama757
Posts: 451
Joined: Sun Nov 06, 2005 12:07 am

Post by krama757 »

Cant you also use mbstowcs() ?
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

This question is frequently asked. May be that would be a good idea to include some very easy conversion's functions for strings in Irrlicht svn version?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Using stringc and stringw this shall be possible (although I have some doubts, because no such conversion methods are present, so I think this simply copies bytes).
benny53
Posts: 131
Joined: Fri May 26, 2006 10:21 pm
Location: Ohio

Post by benny53 »

or do something like this. First,you need a function to convert them,so this would be a function to do that:

Code: Select all

void StringToWide(wchar_t* pOutputString,char* pInputString){ 
     if(pOutputString && pInputString){ 
          std::size_t StringLength=strlen(pInputString); 
          for(std::size_t i=0;i<StringLength+1;i++){ 
               pOutputString[i]=static_cast<wchar_t>(pInputString[i]); 
          }
      }
}
then,to convert all you do is:

Code: Select all

char* me = "hello!"
wchar_t wText[512];
StringToWide(&wText[0],me);
device->setWindowCaption(me);
That should be all you need. So I hope that helps:)

and of course you'll have to use #include <iostream>
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, size_t and strlen should be included by <cstring> so no need for iostream. And static_cast from wchar_t to char will only work for ascii codes, just as the stringw/stringc part. All unicode features will definitely fail.
Post Reply