没有合适的资源?快使用搜索试试~ 我知道了~
深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换
31 下载量 73 浏览量
2021-01-01
03:38:13
上传
评论
收藏 31KB PDF 举报
温馨提示
试读
2页
代码如下: #ifndef USE_H_ #define USE_H_ #include <iostream> #include <windows> #include <string> using namespace std; class CUser { public: CUser(); virtual~ CUser(); char* WcharToChar(const wchar_t* wp); char* StringToChar
资源推荐
资源详情
资源评论
深入理解深入理解c++中中char*与与wchar_t*与与string以及以及wstring之间的之间的
相互转换相互转换
代码如下:
#ifndef USE_H_
#define USE_H_
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
class CUser
{
public:
CUser();
virtual~ CUser();
char* WcharToChar(const wchar_t* wp);
char* StringToChar(const string& s);
char* WstringToChar(const wstring& ws);
wchar_t* CharToWchar(const char* c);
wchar_t* WstringToWchar(const wstring& ws);
wchar_t* StringToWchar(const string& s);
wstring StringToWstring(const string& s);
string WstringToString(const wstring& ws);
void Release();
private:
char* m_char;
wchar_t* m_wchar;
};
#endif;
代码如下:
#include “stdafx.h”
#include “use.h”
CUser::CUser()
:m_char(NULL)
,m_wchar(NULL)
{
}
CUser::~CUser()
{
Release();
}
char* CUser::WcharToChar(const wchar_t* wp)
{
Release();
int len= WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),NULL,0,NULL,NULL);
m_char=new char[len+1];
WideCharToMultiByte(CP_ACP,0,wp,wcslen(wp),m_char,len,NULL,NULL);
m_char[len]=’ ′;
return m_char;
}
wchar_t* CUser::CharToWchar(const char* c)
{
Release();
int len = MultiByteToWideChar(CP_ACP,0,c,strlen(c),NULL,0);
m_wchar=new wchar_t[len+1];
MultiByteToWideChar(CP_ACP,0,c,strlen(c),m_wchar,len);
m_wchar[len]=’\0′;
return m_wchar;
}
void CUser::Release()
{
if(m_char)
{
delete m_char;
m_char=NULL;
资源评论
weixin_38738977
- 粉丝: 6
- 资源: 971
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- python-leetcode面试题解之第157题用Read4读取N个字符-题解.zip
- python-leetcode面试题解之第156题上下翻转二叉树-题解.zip
- python-leetcode面试题解之第155题最小栈-题解.zip
- python-leetcode面试题解之第153题寻找旋转排序数组中的最小值-题解.zip
- python-leetcode面试题解之第152题乘积最大子数组-题解.zip
- python-leetcode面试题解之第151题反转字符串中的单词-题解.zip
- python-leetcode面试题解之第150题逆波兰表达式求值-题解.zip
- python-leetcode面试题解之第149题直线上最多的点数-题解.zip
- python-leetcode面试题解之第148题排序链表-题解.zip
- python-leetcode面试题解之第147题对链表进行插入排序-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功