Char* 转 std::wstring

仅用于 ASCII 或 Latin-1,无法用于多字节字符集代码页

	std::string s("hello world");
	// std::string s("你好,世界"); 不行
	std::wstring ws;
	ws.assign(s.begin(), s.end());
	std::string s("你好,世界");
	std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;

没有限制

	const int buffer_size = 255;

	std::string s("hello, world");
	wchar_t* pwcs = (wchar_t*)malloc(buffer_size * sizeof(wchar_t));
	size_t converted = 0;
	mbstowcs_s(&converted, pwcs, buffer_size, s.c_str(), s.length());

	if (pwcs != 0)
		std::wcout << std::wstring(pwcs);

	free(pwcs);
	std::string str("你好,世界");
	std::vector<wchar_t> buf(str.size());
	std::use_facet<std::ctype<wchar_t>>(std::locale()).widen(str.data(), str.data() + str.size(), buf.data());

	std::wcout << std::wstring(buf.data(), buf.size());

你可能感兴趣的:(C++)