Jump to content
  • Advertisement
Sign in to follow this  
Sionice

Simple Conversion Question

This topic is 5010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Trying to convert a WCHAR to a char *. I've just never dealt with WCHAR's before. assume I have this code char *myName = "blah"; WCHAR wstr[MAX_PATH] = myName; I get this error for obvious reasons: error C2664: 'LoadObjectFromFile' : cannot convert parameter 3 from 'unsigned short *[260]' to 'unsigned short *' thanks ^_^

Share this post


Link to post
Share on other sites
Advertisement
MultiByteToWideChar() and WideCharToMultiByte() are what you would use (in Windows, at least) to convert back and forth. Here's some code that's just slightly modified from what I've used before:
void LPSTR_To_LPWSTR(LPCSTR str, LPWSTR& wstr)
{
int StringLength;
int BufferLength;
StringLength = strlen(str);
BufferLength = MultiByteToWideChar(CP_THREAD_ACP, 0, str, StringLength, NULL, 0);
wstr = new WCHAR[BufferLength];
MultiByteToWideChar(CP_THREAD_ACP, 0, str, StringLength, wstr, BufferLength);
}

void LPWSTR_To_LPSTR(LPCWSTR wstr, LPSTR& str)
{
int StringLength;
int BufferLength;
StringLength = wcslen(wstr) + 1;
BufferLength = WideCharToMultiByte(CP_THREAD_ACP, 0, wstr, StringLength, NULL, 0, NULL, NULL);
str = new CHAR[BufferLength];
WideCharToMultiByte(CP_THREAD_ACP, 0, wstr, StringLength, str, BufferLength, NULL, NULL);
}


I'm sure there are other ways to do this too, such as through the STL with std::string and std::wstring and stuff that I haven't found yet, but this is the only way that I know of at the moment; it gets the job done.

[edit]Don't forget to delete the memory when you're done with it. I suppose you could modify the functions to just copy to buffers that already exist, in order to be able to use statically allocated memory instead... That might be more useful.[/edit]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!