惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News
H
Help Net Security
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
T
Troy Hunt's Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
S
Secure Thoughts
Y
Y Combinator Blog
D
Docker
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
N
News and Events Feed by Topic
aimingoo的专栏
aimingoo的专栏
I
InfoQ
P
Palo Alto Networks Blog
F
Full Disclosure
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Heimdal Security Blog
G
Google Developers Blog
Webroot Blog
Webroot Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
WordPress大学
WordPress大学
W
WeLiveSecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
Help Net Security
Help Net Security
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
博客园 - 三生石上(FineUI控件)
T
Threatpost
V
V2EX
AWS News Blog
AWS News Blog
O
OpenAI News
V
Visual Studio Blog

博客园 - surfer

金山开源安全卫士全套代码编译指南 TabControl添加关闭按钮 string、wstring、cstring、 char、 tchar、int、dword转换方法 在VS 2008下成功编译Chrome QT + VS2008安装配置 用于多媒体应用的无窗口ATL ActiveX控件 临界区,互斥量,信号量,事件的区别(线程同步) GuiToolKit的编译 在VC程序中调用exe文件或者批处理文件方法总结 可在U盘或光盘上运行的LINUX(自带强大软件包和开发工具) 用VC纯资源dll制作多语言界面程序 - surfer - 博客园 没有完美的外汇经纪商和交易平台 外汇交易时段 MB Trading 订单类型 外汇炒单速成法 外汇高手的顺势理论 外汇点值计算 外汇交易商排名 explorer无法随Windows 7启动原因
string,wstring,CString,TCHAR,char*之间常用转换
surfer · 2011-11-24 · via 博客园 - surfer

因为自己比较健忘,所以保存下来以备不时之需。



***********************************************************************
* 函数: TransCStringToTCHAR
* 描述:将CString 转换为 TCHAR*
* 日期:
***********************************************************************
TCHAR* CString2TCHAR(CString &str)
{
int iLen = str.GetLength();
TCHAR* szRs = new TCHAR[iLen];
lstrcpy(szRs, str.GetBuffer(iLen));
str.ReleaseBuffer();
return szRs;
}
***********************************************************************
* 函数: THCAR2Char
* 描述:将TCHAR* 转换为 char*
***********************************************************************
char* THCAR2char(TCHAR* tchStr)
{
int iLen = 2*wcslen(tchStr);//CString,TCHAR汉字算一个字符,因此不用普通计算长度
char* chRtn = new char[iLen+1]
wcstombs(chRtn,tchStr,iLen+1);//转换成功返回为非负值
return chRtn;
}
***********************************************************************
* 函数: CString2char
* 描述:将CString转换为 char*
***********************************************************************
char* CString2char(CString &str)
{
int len = str.GetLength();
char* chRtn = (char*)malloc((len*2+1)*sizeof(char));//CString的长度中汉字算一个长度
memset(chRtn, 0, 2*len+1);
USES_CONVERSION;
strcpy((LPSTR)chRtn,OLE2A(str.LockBuffer()));
return chRtn;
}
***********************************************************************
* 函 数 名:GetAnsiString
* 描 述:将CString(unicode)转换为char*(ANSI)
* 参 数:CString &s 要转换的CString
* 返 回 值:返回转换结果
***********************************************************************
char* GetAnsiString(const CString &s)
{
int nSize = 2*s.GetLength();
char *pAnsiString = new char[nSize+1];
wcstombs(pAnsiString, s, nSize+1);
return pAnsiString;
}

/***********************************************************************/
/*string转wstring*/
/***********************************************************************/
#include <string>
#include <locale.h>
using namespace std;

wstring str2wstr(const string &str)
{
    setlocale(LC_ALL, "chs");
    const char* _Source = str.c_str();
    size_t _Dsize = str.size() + 1;
    wchar_t *_Dest = new wchar_t[_Dsize];
    wmemset(_Dest, 0, _Dsize);
    mbstowcs(_Dest,_Source,_Dsize);
    wstring result = _Dest;
    delete []_Dest;

    setlocale(LC_ALL, "C");

    return result;
}

/***********************************************************************/
/*wstring转string*/
/***********************************************************************/
#include <string>
#include <locale.h>
using namespace std;

string wstr2str(const wstring &wstr)
{

    string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";

    setlocale(LC_ALL, "chs");

    const wchar_t* _Source = wstr.c_str();
    size_t _Dsize = 2 * wstr.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest,0,_Dsize);
    wcstombs(_Dest,_Source,_Dsize);
    string result = _Dest;
    delete []_Dest;

    setlocale(LC_ALL, curLocale.c_str());

    return result;
}

/***********************************************************************/
/*string转wstring*/
/***********************************************************************/
#include <string>
using namespace std;
string char2str(char *arr,int count)
{
    string result(arr,count);
    return result;
}