Skip to main content
GameDev.net gamedev.net
🔒 Locked

[c++] wstring conversion

Started by Yourself Dec 29, 2008 at 10:44 AM 5 replies 6k views
Original Post
Yourself
Yourself
hi, I want to convert a wstring to a const char* (and visa verse) . the problem is that I use 2 different libs, to one uses wstrings, the other const char * .... I tried using the MultiByteToWideChar() but I couldn't get it working ... can you guys help me out ? thx
SiCrane
SiCrane
There are two necessary steps for converting a string: allocating a buffer and then performing the actual conversion. For a narrow string to a wide string those two steps could be accomplished like so:
#include <cstdlib>#include <vector>#include <string>#include <iostream>int main(int, char **) {  std::string str = "Moo! Moo, I Say!";    std::vector<wchar_t> buffer(mbstowcs(0, str.c_str(), 0) + 1); // allocate the buffer  mbstowcs(&buffer[0], str.c_str(), buffer.size());  // perform the conversion    std::wcout << &buffer[0];  return 0;}

To go from a wide string to a narrow string you could use wcstombs() instead of mbstowcs().
Yourself
Yourself
thx !

but I can't get it working to narrow the string ...

I used wcstombs() as you said, but it ain't working..

//name == wstring, parameter of functionvector<char> buffer(wcstombs(0, name.c_str(), 0) + 1); // allocate the bufferwcstombs(&buffer[0], name.c_str(), buffer.size());  // perform the conversionm_pActor->setName(&buffer[0]); // actor is PhysX : virtual void setName (const char *name)=0 


what I'm I doing wrong ?
KulSeran
KulSeran
likely your vector is going out of scope, thus your m_pActor->setName() is pointing at garbage.

(unless aint working means something else. Please be more specific)
Yourself
Yourself
well, it is pointing at garbage, that's the problem...
Quote:

ex :
name = box01
but the name of the actor is set to 'îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþ£Öú#uë'

see ?
I want to convert a wstring so I can use it as argument of setname(const char* name)...

how can I convert this correctly ?

thx
taby
taby
If you figure you're going to be dealing with ASCII-ish characters (e.g.: English alphabet, Arabic numerals, slashes, periods, etc, but NOT Japanese characters), then you can probably go the easy route:

#include <iostream>using std::cout;using std::wcout;#include <string>using std::string;using std::wstring;int main(void){{	// Convert from wide to regular:	wstring ws = L"ooga";	string s(ws.begin(), ws.end());	cout << s.c_str() << '\n';// Example of const char* via .c_str()}{	// Convert from regular to wide:	string s = "ooga";	wstring ws(s.begin(), s.end());	wcout << ws.c_str() << L'\n'; // Example of const wchar_t* via .c_str()}	return 0;}


If for some reason you're using something like Japanese characters, then converting to regular strings is a bad idea from the start.

[Edited by - taby on December 29, 2008 6:31:09 PM]
Antheus
Antheus
Quote:
Original post by Yourself
thx !

but I can't get it working to narrow the string ...

I used wcstombs() as you said, but it ain't working..

//name == wstring, parameter of functionvector<char> buffer(wcstombs(0, name.c_str(), 0) + 1); // allocate the bufferwcstombs(&buffer[0], name.c_str(), buffer.size());  // perform the conversionm_pActor->setName(&buffer[0]); // actor is PhysX : virtual void setName (const char *name)=0 


This one?

If so, then you have two ways:
- pass string literal: m_actor->setName("SomeName");
- allocate new string to store by setName:
const char * s = new char[buffer.size()+1];strcpy(s, &buffer[0]);m_actor->setName(s);....// when destroying m_actordelete[] m_actor->getName();

- same as before, but don't bother with deleting and just leak memory

But like documentation says, that's debugging function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.