Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a "pointer to const TCHAR" to a "std::string"?

I have a class which returns a typed pointer to a "const TCHAR". I need to convert it to a std::string but I have not found a way to make this happen.

Can anyone provide some insight on how to convert it?

like image 573
TERACytE Avatar asked Dec 03 '22 06:12

TERACytE


2 Answers

Depending on your compiling settings, TCHAR is either a char or a WCHAR (or wchar_t).

If you are using the multi byte character string setting, then your TCHAR is the same as a char. So you can just set your string to the TCHAR* returned.

If you are using the unicode character string setting, then your TCHAR is a wide char and needs to be converted using WideCharToMultiByte.

If you are using Visual Studio, which I assume you are, you can change this setting in the project properties under Character Set.

like image 105
Brian R. Bondy Avatar answered Dec 28 '22 13:12

Brian R. Bondy


Do everything Brian says. Once you get it in the codepage you need, then you can do:

std::string s(myTchar, myTchar+length);

or

std::wstring s(myTchar, myTchar+length);

to get it into a string.

like image 38
McBeth Avatar answered Dec 28 '22 15:12

McBeth