• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ProgrammerZ

convert char* to LPCWSTR

8 posts in this topic

Hey, I was just wondering if there is an easy way to convert a char* to a LPCWSTR. I'd also like to know how to convert a char* to a std::string, a LPCWSTR to a std::string, a std::string to a char*, etc. --ProgrammerZ
0

Share this post


Link to post
Share on other sites

// Convert char_array to std::string (It is done automatically with the strings = overload)
char* char_array = "Foo";
std::string str = char_array;

// Convert std::string to char array
std::string str = "Bar";
char* char_array = str.c_str();


I really have no idea about LPCWSTR...

Edit:
It looks like the LPCWSTR is just a wide character char array. I'm not really sure why you would need to convert back and forth. Generally you would use either Single byte characters or Multibyte. You can choose to always use Multibyte in the Project properties in Visual Studio IIRC.
0

Share this post


Link to post
Share on other sites
A LPCWSTR is a "Long Pointer to Const Wide String". To convert from a multibyte string (char *) to a unicode string (wchar_t *) you will need to use the function mbstowcs or MultiByteToWideChar.

This page should have the examples you need for a MultiByteToWideChar example. This page on MSDN has an example for mbstowcs.

To get a LPCWSTR to a std::string, you convert it to a multibyte string first (char *) and then save the std::string from the char * as Sr_Guapo has shown, but you have to be careful of the 'const' pointer conversions and type cast yourself.
0

Share this post


Link to post
Share on other sites
Adapt these as you see fit.


// ----------------------------------------------------------------------------
//
//
BOOL WINAPI UnicodeToAnsi(
LPWSTR pszwUniString,
LPSTR pszAnsiBuff,
DWORD dwAnsiBuffSize
)
{
int iRet = 0;
iRet = WideCharToMultiByte(
CP_ACP,
0,
pszwUniString,
-1,
pszAnsiBuff,
dwAnsiBuffSize,
NULL,
NULL
);
return ( 0 != iRet );
}


// ----------------------------------------------------------------------------
//
//
BOOL WINAPI AnsiToUnicode(
LPSTR pszAnsiString,
LPWSTR pszwUniBuff,
DWORD dwUniBuffSize
)
{

int iRet = 0;
iRet = MultiByteToWideChar(
CP_ACP,
0,
pszAnsiString,
-1,
pszwUniBuff,
dwUniBuffSize
);

return ( 0 != iRet );
}

0

Share this post


Link to post
Share on other sites
char* to LPCWSTR is going to be quite painful. I suggest using a wstring wherever you can, and using wstring::c_str() to convert to LPCWSTR (which is just a const wchar_t*) whenever you need it.


std::wstring ToWideString(const std::string& str)
{
int stringLength = MultiByteToWideChar(CP_ACP, 0, str.data(), str.length(), 0, 0);
std::wstring wstr(stringLength, 0);
MultiByteToWideChar(CP_ACP, 0, str.data(), str.length(), &wstr[0], stringLength);
return wstr;
}


That's untested and written off the top of my head, but it should work (I think). If not, that's the general gist of it.

EDIT: Wow, I'm slow. [lol]
0

Share this post


Link to post
Share on other sites
Because the ASCII character set is a subset of most wide character sets, and still have the values 0-255 (the higher-order byte is just 0), this would work.

std::string charstring = "hello, world";

std::wstring widestring;

for (int i = 0; i < (int)charstring.length(); i++)
widestring += (wchar_t)charstring[i];

LPCWSTR lpcwstr = widestring.c_str();


Although, LessBread's method is probably safer to use.
0

Share this post


Link to post
Share on other sites
You want to use "wchar_t" instead of "char" if you need a LPCWSTR. LPCWSTR is a pointer to a wide char array (Long Pointer to Const Wide String). You can also change your project to not use Unicode or use _TCHAR instead of "char" which will automatically become "wchar_t" if unicode is defined and "char" if unicode is not defined.
0

Share this post


Link to post
Share on other sites
You can also use L as a prefix to identify a wchar_t* string literal.

Eg:
const wchar_t* some_text = L"blah";
if(some_text[3] != wchar_t('a')) throw wobbly;

NOTE:
I'm not sure where this feature is defined.
It works in MSVC, I believe it works in g++.
It's probably part of the C++ standard somewhere but I'm not 100% sure where to look.

ASIDE:
I've seen that there are proposals for C++0x for the syntax:
int* digits = <int>"510034222";
or
int* digits = int"510034222";
Would be great to have this feature in C++.


0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0