(环境:Win7 64位, C++ builder 2010)
C/C++中一般会使用到std::string,但是很少用到std::wstring,一旦使用到就会涉及多字节和宽字节的相互转换。
这里使用到的API为MultiByteToWideChar()和WideCharToMultiByte(),处理时,一般先获取字符长度,然后再根据长度申请内存,最后赋值。示例代码如下:
#include
#include
bool String2WString(const std::string &strSrc, std::wstring &strDest)
{
strDest.clear();
if (strSrc.empty())
{
return true;
}
bool bResult = false;
int iLen = MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), strSrc.length(), NULL, NULL);
if (iLen == 0)
{
return bResult;
}
wchar_t *pszWBuffer = new wchar_t[iLen + 1];
int iSize = (iLen + 1) * sizeof(wchar_t);
memset(pszWBuffer, 0, iSize);
iLen = MultiByteToWideChar(CP_
最后
以上就是害羞小馒头最近收集整理的关于c 语言里如何将int换成wstring,C/C++:关于std::string与std::wstring的转换的全部内容,更多相关c内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复