# using strings in DrawText

## Recommended Posts

pteri498    122
I'm trying to plug std::string into the LPD3DXFONT::DrawText function. I can't seem to find a proper method of inputting my string into the DrawText function. Here's my code:
g_font->DrawText(g_sprite, sFPS.c_str(), -1, &rText, DT_LEFT|DT_NOCLIP, 0xFF000000)


That produces the following error: error C2664: 'ID3DXFont::DrawTextW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR' Now I think this may have something to do with the unicode nature of my coding. I can't just turn off unicode in my project properties because I've built all my code around unicode compatibility, and to turn that off invalidates a lot of my code. Does anyone know a good way to turn my string into something more LPCWSTR compatible?

##### Share on other sites
__ODIN__    479
The compiler assumes this is a unicode build, and typedefs DrawText to DrawTextW. Try using DrawTextA instead of DrawText.

g_font->DrawTextA(g_sprite, sFPS.c_str(), -1, &rText, DT_LEFT|DT_NOCLIP, 0xFF000000)

Allan

##### Share on other sites
circlesoft    1178
If at some time you actually want to convert to and from actual wide-character (aka unicode) strings, you have these APIs available:

- MultiByteToWideChar()
- WideCharToMultiByte()

##### Share on other sites
pteri498    122
But then I lose support for inputting unicode characters, right? Is there any conversion so I can put this string into a unicode function? Maybe something that will convert the string to a wchar_t?

##### Share on other sites
Evil Steve    2017
If you're using unicode, you should be using std::wstring. Alternatively, the function that circlesoft mentioned (MultiByteToWideChar()) will do the job

##### Share on other sites
__ODIN__    479
This snippet should work.

There's a couple of optimizations available (drop the copy-constructor by passing the wstring by reference), and whether you want to create a hard limit for the number of characters is a personal choice.

std::wstring		ConvertAtoW(const std::string& str){	if (str.empty()) return L"";	const unsigned short	UNICODE_C = 1024;	unsigned short	UnicodeA[UNICODE_C ];	size_t	StrLen = str.size();	assert(StrLen < UNICODE_C); 	memset(UnicodeA, 0, sizeof(UnicodeA));	MultiByteToWideChar(CP_UTF8, 0,str.c_str(), StrLen, UnicodeA, UNICODE_C);	std::wstring	Uni = std::wstring(UnicodeA);	return Uni;}

Allan