This question has been raised many times and I'm raising it again ;]
I have found many article on how people accomplish the conversion but none of them have gotten me any further.
I have a function which returns a const char * which I am putting into an IGUIComboBox* and I need to convert it into a const wchar_t * to do so.
Most of the articles I've found the code seems excessive and in many places they talk about memory leaks.
I want to ask here so I get it right the first time and can move on in my project :)
As always I thank the community for their much appreciated help ^.^
const char * to const wchar_t *
const char * to const wchar_t *
Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.
Re: const char * to const wchar_t *
irrlicht strings make it easy to convert. Just pass one or the other in to the stringc and stringw classes
make a byte string
stringc s("this is byte character");
// make a wide string
stringw w(L"this is wide character");
convert byte to wide
stringw btw(s);
convert wide to byte
stringc wtb(w);
then use with ######.c_str();
make a byte string
stringc s("this is byte character");
// make a wide string
stringw w(L"this is wide character");
convert byte to wide
stringw btw(s);
convert wide to byte
stringc wtb(w);
then use with ######.c_str();
Re: const char * to const wchar_t *
Oh that's amazing, simply amazing :D
What do I need to include to access the btw function?
Assuming, based off what you said, it is a part of Irrlicht, I tried btw, irr::btw, irr::core::btw, neither of which have a member function btw :(
What do I need to include to access the btw function?
Assuming, based off what you said, it is a part of Irrlicht, I tried btw, irr::btw, irr::core::btw, neither of which have a member function btw :(
Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.
Re: const char * to const wchar_t *
sorry, i got too cute, let me back up
define a stringc variable called anything you wnat, I call mine s
stringc s; // now, the variable s is an empty stringc (byte char string) this is BYTE
I could pass in a string to set the stringc to when i create it like this
stringc s("this is some text that is stored in the stringc variable called s"); this is BYTE
I can also create a wide character string ( in my case I will call it ws )
stringw ws; // now, the variable ws is an empty stringw (wide char string) // this is WIDE
I could pass in a string to set the stringw to when i create it like this
stringw ws(L"this is some text that is stored in the stringw variable called ws"); // this is WIDE
if I need to convert the BYTE to WIDE, i can simply do this
stringw cs(s); // pass the stringc varaible s (BYTE) to the stringw variable cs (WIDE) and it auto converts for you
the variable cs now has the same text as the variable s except it is in wide format.
or if I need to convert the WIDE to BYTE, i can simply do this
stringc cs(ws); // pass the stringw variable ws (WIDE) to the stringc variable cs (BYTE) and it auto converts for you
the variable cs now has the same text as the variable ws except it is in byte format.
define a stringc variable called anything you wnat, I call mine s
stringc s; // now, the variable s is an empty stringc (byte char string) this is BYTE
I could pass in a string to set the stringc to when i create it like this
stringc s("this is some text that is stored in the stringc variable called s"); this is BYTE
I can also create a wide character string ( in my case I will call it ws )
stringw ws; // now, the variable ws is an empty stringw (wide char string) // this is WIDE
I could pass in a string to set the stringw to when i create it like this
stringw ws(L"this is some text that is stored in the stringw variable called ws"); // this is WIDE
if I need to convert the BYTE to WIDE, i can simply do this
stringw cs(s); // pass the stringc varaible s (BYTE) to the stringw variable cs (WIDE) and it auto converts for you
the variable cs now has the same text as the variable s except it is in wide format.
or if I need to convert the WIDE to BYTE, i can simply do this
stringc cs(ws); // pass the stringw variable ws (WIDE) to the stringc variable cs (BYTE) and it auto converts for you
the variable cs now has the same text as the variable ws except it is in byte format.
Re: const char * to const wchar_t *
use them as wide or byte like this
stringc cs("my little test string");
printf("this is the byte string variable cs - %s\n", cs.c_str());
will print to the screen this is the byte string variable cs - my little test string
since printf wont print wide characters, you can use our little trick above to do this
stringw ws(L" this is some wide character text");
stringc cs(ws); // convert the wide to byte
printf("this is the wide text converted to byte - %s\n", cs.c_str());
or, in less code
stringw ws(L" this is some wide character text");
printf("this is the wide text converted to byte - %s\n", stringc(ws).c_str());
hope this makes sense.
Seven
stringc cs("my little test string");
printf("this is the byte string variable cs - %s\n", cs.c_str());
will print to the screen this is the byte string variable cs - my little test string
since printf wont print wide characters, you can use our little trick above to do this
stringw ws(L" this is some wide character text");
stringc cs(ws); // convert the wide to byte
printf("this is the wide text converted to byte - %s\n", cs.c_str());
or, in less code
stringw ws(L" this is some wide character text");
printf("this is the wide text converted to byte - %s\n", stringc(ws).c_str());
hope this makes sense.
Seven
Last edited by Seven on Mon Oct 14, 2013 8:51 pm, edited 1 time in total.
Re: const char * to const wchar_t *
Thank you very much for the clarification my kind sir :)
Here is the completed function for anyone else who may stumble upon this ;]
Here is the completed function for anyone else who may stumble upon this ;]
Code: Select all
irr::gui::IGUIComboBox *ComboBox = AppContext.GUIEnv->addComboBox(irr::core::rect<irr::s32>(10,25,LoginWindowSize.X-10,40),MainWindow,-1);
unsigned int CharNum = 1;
for(std::vector<std::string>::iterator it = AppContext.LocalPlayer.CharacterNames.begin(); it != AppContext.LocalPlayer.CharacterNames.end(); ++it)
{
ComboBox->addItem( irr::core::stringw((*it).c_str()).c_str(), CharNum ); //Thanks Seven!
++CharNum;
}Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.
Re: const char * to const wchar_t *
Code: Select all
ComboBox->addItem( irr::core::stringw((*it)).c_str(), CharNum ); //Thanks Seven!
Re: const char * to const wchar_t *
I tried that at first and received this error
I suppose I should rephrase my original question to "std::string to const wchar_t *"
Code: Select all
error C2440: '<function-style-cast>' : cannot convert from 'std::basic_string<_Elem,_Traits,_Alloc>' to 'irr::core::stringw'Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.
Re: const char * to const wchar_t *
std:string tempstring("hello world");
stringw convertedString( tempstring().buffer());
or something like.....
ComboBox->addItem( irr::core::stringw((*it)).buffer(), CharNum ); //Thanks Seven!
stringw convertedString( tempstring().buffer());
or something like.....
ComboBox->addItem( irr::core::stringw((*it)).buffer(), CharNum ); //Thanks Seven!