codeLover
codeLover

Reputation: 3810

How to convert TChar[] array to std::string in VC++. Help on syntax

Hi I have an array of type TCHAR arr[256]. I want to convert this TChar array to a std::string, so that I will able use the functions in the string class.

Can anyone kindly let me know how to do this TCHAR[] to string conversion in VC++.

Also what is the syntax to do this:

If TCHAR arr[256] is my array. 
std::string str is my straing obj

. Will the foll syntax work fine: str = arr;

Thanks in advance.

Upvotes: 2

Views: 11877

Answers (1)

Ulterior
Ulterior

Reputation: 2912

Usually I do it like this:

TCHAR arr[256];
std::wstring arr_w( arr );
std::string arr_s( arr_w.begin(), arr_w.end() );

result is in arr_s

Dont expect any chars outside standart ASCII to be converted though

Upvotes: 2

Related Questions