[英]How do I convert a string to a wstring using the value of the string?
我是 C++ 的新手,我有这个问题。 我有一个名为 DATA_DIR 的字符串,我需要将其格式化为 wstring。
string str = DATA_DIR;
std::wstring temp(L"%s",str);
Visual Studio 告诉我没有与参数列表匹配的构造函数实例。 显然,我做错了什么。
我在网上找到了这个例子
std::wstring someText( L"hello world!" );
这显然有效(没有编译错误)。 我的问题是,如何将存储在 DATA_DIR 中的字符串值放入 wstring 构造函数而不是像“hello world”这样的任意东西?
这是使用wcstombs
的实现(已更新):
#include <iostream> #include <cstdlib> #include <string> std::string wstring_from_bytes(std::wstring const& wstr) { std::size_t size = sizeof(wstr.c_str()); char *str = new char[size]; std::string temp; std::wcstombs(str, wstr.c_str(), size); temp = str; delete[] str; return temp; } int main() { std::wstring wstr = L"abcd"; std::string str = wstring_from_bytes(wstr); }
这是关于投票率最高的答案的参考,但是我没有足够的“声誉”来直接对答案发表评论。
解决方案中函数的名称“ wstring_from_bytes”表示它正在执行原始发布者想要的操作,即在给定字符串的情况下获取wstring,但是该函数实际上与原始发布者要求的相反,并且将更准确被命名为“ bytes_from_wstring”。
若要从字符串转换为wstring,wstring_from_bytes函数应使用mbstowcs而不是wcstombs
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdlib>
#include <string>
std::wstring wstring_from_bytes(std::string const& str)
{
size_t requiredSize = 0;
std::wstring answer;
wchar_t *pWTempString = NULL;
/*
* Call the conversion function without the output buffer to get the required size
* - Add one to leave room for the NULL terminator
*/
requiredSize = mbstowcs(NULL, str.c_str(), 0) + 1;
/* Allocate the output string (Add one to leave room for the NULL terminator) */
pWTempString = (wchar_t *)malloc( requiredSize * sizeof( wchar_t ));
if (pWTempString == NULL)
{
printf("Memory allocation failure.\n");
}
else
{
// Call the conversion function with the output buffer
size_t size = mbstowcs( pWTempString, str.c_str(), requiredSize);
if (size == (size_t) (-1))
{
printf("Couldn't convert string\n");
}
else
{
answer = pWTempString;
}
}
if (pWTempString != NULL)
{
delete[] pWTempString;
}
return answer;
}
int main()
{
std::string str = "abcd";
std::wstring wstr = wstring_from_bytes(str);
}
无论如何,这在标准库的较新版本(C ++ 11和更高版本)中更容易完成
#include <locale>
#include <codecvt>
#include <string>
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wide = converter.from_bytes(narrow_utf8_source_string);
printf
样式的格式说明符不是C ++库的一部分,不能用于构造string
。
如果string
只能包含单字节字符,则范围构造函数就足够了。
std::string narrower( "hello" );
std::wstring wider( narrower.begin(), narrower.end() );
问题是,当适用宽字符时,我们通常使用wstring
(因此为w
),这些std::string
在std::string
由多字节序列表示。 这样做将导致多字节序列的每个字节转换为不正确的宽字符序列。
此外,要转换多字节序列,需要知道其编码。 此信息未由std::string
或std::wstring
封装。 C ++ 11允许您使用std::wstring_convert
指定编码和转换,但是我不确定它支持的范围。 参见0x...。
我发现了这个功能。 找不到任何预定义的方法来执行此操作。
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
std::wstring stemp = s2ws(myString);
针对 C++11 及更高版本提到的转换器已弃用 C++17 中的此特定转换,并建议使用 MultiByteToWideChar 函数。
编译器错误 (c4996) 提到定义 _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING。
wstring temp = L"";
for (auto c : DATA_DIR)
temp.push_back(c);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.