我是靠谱客的博主 害羞小馒头,最近开发中收集的这篇文章主要介绍c 语言里如何将int换成wstring,C/C++:关于std::string与std::wstring的转换,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

(环境: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 语言里如何将int换成wstring,C/C++:关于std::string与std::wstring的转换所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(367)

评论列表共有 0 条评论

立即
投稿
返回
顶部