//// convert_cstring_and_wstring.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
////
//
//#include // MFC core and standard components
//#include
//#include
//#include // CString header
//
//void ConvertAndPrint()
//{
// // std::wstring to CString
// std::wstring wstr = L"Hello, World!";
// CString cstr(wstr.c_str());
// std::wcout << L"Converted CString: " << cstr.GetString() << std::endl;
//
// // CString to std::wstring
// CString cstr2 = _T("Another String!");
// std::wstring wstr2(cstr2.GetString());
// std::wcout << L"Converted std::wstring: " << wstr2 << std::endl;
//}
//
//int main()
//{
// ConvertAndPrint();
// return 0;
//}
include
include
include
include
std::string WStringToString(const std::wstring& wstr)
{
// Use a wstring_convert object to convert wstring to string
std::wstring_convertstd::codecvt_utf8<wchar_t> converter;
return converter.to_bytes(wstr);
}
void Example()
{
std::wstring wstr = L"Hello, World!";
std::string str = WStringToString(wstr);
std::cout << "Converted std::string: " << str << std::endl;
}
int main()
{
Example();
}
Top comments (0)