Taking the advice from a previous post, I have started using the std::string class, along with string streams. This is VERY useful, and very easy to use.
The time has come for me to add a simple GUI to my game, and, of course, cannot use string.
All of the GUI elements use wchar_t* as their format. How do you get this format from std::string?
using std::string with gui elements
There is a core::stringw class which holds wide character strings, and you can get a constant array of its characters with string.c_str(), however, I have had no luck in getting text into a GUI element either, even with this method 
.: http://www.mercior.com :.
wchar_t* is a Wide Character Pointer, while a char* is a Multibyte Character Pointer.
The functions to go between the two are mbstowcs() and wcstombs().
Alternately, you can use mbtowc() and wctomb() to only convert 1 character.
This is one way I do it. Another way would be to malloc the storage size. Since c_str() just returns a const char*, replace somechar* with your c_str()'ed string
The functions to go between the two are mbstowcs() and wcstombs().
Alternately, you can use mbtowc() and wctomb() to only convert 1 character.
This is one way I do it. Another way would be to malloc the storage size. Since c_str() just returns a const char*, replace somechar* with your c_str()'ed string
Code: Select all
wchar_t ConvText[255];
mbstowcs(ConvText, somechar*,255);
Crud, how do I do this again?
-
agrif
Thanks. I looked up mbstowcs() on google, and used some examples.
However, when I compile, there are no errors, but then I run it, and it gives me a nice crash.
Here is my code, but I dunno if I'm doing anything wrong.
I am using the latest version of Dev-C++, if that has anything to do with it.
agrif
However, when I compile, there are no errors, but then I run it, and it gives me a nice crash.
Here is my code, but I dunno if I'm doing anything wrong.
Code: Select all
// tester! yay!
#include <iostream>
using namespace std;
int main() {
cout << "type a string to convert!" << endl << ">: ";
string str("");
cin >> str;
wchar_t *charw;
mbstowcs(charw, str.c_str(), 255);
cout << "Wchar'd string: " << charw << endl;
return 0;
}agrif
agrif wrote:Thanks. I looked up mbstowcs() on google, and used some examples.
However, when I compile, there are no errors, but then I run it, and it gives me a nice crash.
Here is my code, but I dunno if I'm doing anything wrong.
Yes you do
See:
Code: Select all
// tester! yay!
#include <iostream>
using namespace std;
int main() {
cout << "type a string to convert!" << endl << ">: ";
string str("");
cin >> str;
wchar_t *charw;
mbstowcs(charw, str.c_str(), 255);
Change that to:
Code: Select all
wchar_t charw[255];
mbstowcs(charw, str.c_str(), 255);
HTH,
Tels
Perl + Irrlicht + Audiere = Game: http://bloodgate.com/perl/game
-
agrif
Use Perl :)agrif wrote:when I do that, it prints out the location in the memory of the computer, not the actual value. I also figured that I would do it that way, because the description I found online was:
mbstowcs(wchar_t *pwcs, const char *s, size_t n)
I am starting to REALLY hate pointers :) .
agrif
Anyway, look up what the string class does and what function it provides. I couldnt find it via man on my system, and I don't have any C++ environment AFAIK. You want a member of str that returns you a pointer to the actual string contents.
Best wishes,
Tels
Perl + Irrlicht + Audiere = Game: http://bloodgate.com/perl/game