C++ 将std::string转换为wchar\u t的typedef*
我正在通过控制台从用户读取文件目录,必须将值存储在C++ 将std::string转换为wchar\u t的typedef*,c++,typedef,wchar-t,C++,Typedef,Wchar T,我正在通过控制台从用户读取文件目录,必须将值存储在pxcCHAR*变量中,该变量是wchar.*的SDKtypedef 我发现我可以通过执行以下操作将std::string转换为std::wstring #include <iostream> int main(){ std::string stringPath; std::getline(std::cin, stringPath); std::wstring wstrPath = std::wstring(
pxcCHAR*
变量中,该变量是wchar.*
的SDKtypedef
我发现我可以通过执行以下操作将std::string
转换为std::wstring
#include <iostream>
int main(){
std::string stringPath;
std::getline(std::cin, stringPath);
std::wstring wstrPath = std::wstring(stringPath.begin(), stringPath.end());
const wchar_t* wcharPath = wstrPath.c_str();
return 0;
}
连接到wcharPath
前面的值来自哪里
此外,
因为pxcCHAR*
是wchar\u t*
的typedef
,所以我认为可以简单地这样做:
pxcCHAR* mFilePath = wcharPath;
但是,我收到一条消息说“const wchar_t*”不能用于初始化类型为“pxcCHAR*”的实体
我期望隐式转换工作,但事实并非如此。如何克服此错误?使用std::wstring(stringPath.begin(),stringPath.end())处理字符串转换是错误的,除非您可以保证只处理7bit ASCII数据(文件系统不是这样)。这种类型的转换根本不考虑字符编码。将std::string
转换为std::wstring
的正确方法是使用std::wstring\u convert
、MultiByteToWideChar()
或其他等效工具。如果你环顾四周,有很多这样的例子
最好只使用std::wstring
开始,而不是std::string
,例如:
#include <iostream>
#include <string>
int main(){
std::wstring stringPath;
std::getline(std::wcin, stringPath);
const wchar_t* wcharPath = stringPath.c_str();
return 0;
}
使用std::wstring(stringPath.begin(),stringPath.end())
处理字符串转换是错误的,除非您可以保证只处理7bit ASCII数据(文件系统不是这样)。这种类型的转换根本不考虑字符编码。将std::string
转换为std::wstring
的正确方法是使用std::wstring\u convert
、MultiByteToWideChar()
或其他等效工具。如果你环顾四周,有很多这样的例子
最好只使用std::wstring
开始,而不是std::string
,例如:
#include <iostream>
#include <string>
int main(){
std::wstring stringPath;
std::getline(std::wcin, stringPath);
const wchar_t* wcharPath = stringPath.c_str();
return 0;
}
阅读。你应该使用
也就是说,您不太可能需要这样做(并且对于任何不需要这样做的代码页,结果很可能是不正确的)。您可能需要在任何地方使用宽字符串
哦,读一读。读一读。你应该使用
也就是说,您不太可能需要这样做(并且对于任何不需要这样做的代码页,结果很可能是不正确的)。您可能需要在任何地方使用宽字符串
哦,请阅读。在讨论字符串类型之间的“转换”时,可能重复没有意义,而不指定转换的任何一方使用的编码。在讨论字符串类型之间的“转换”时,可能重复没有意义,而不指定转换的任何一方使用的编码。
pxcCHAR* mFilePath = const_cast<wchar_t*>(wcharPath);