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

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

博客园 - 转角遇到你

SQL安装问题:SQL Redist ANSI 和 UNICODE 的函数对应表【转】 [转]宽字符处理函数函数与普通函数对照表 cad pk Microstation LPCTSTR类型 UNICODE下宽字符的CString转换为const char * Windows应用程序中几种特殊鼠标事件的识别【转】 在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern “C”?----转 [转] #ifndef#define#endif的用法(整理) cximage用的过程中的错误 【转】三维形体模型的表示 - 转角遇到你 AutoCAD的坐标系 AutoCAD数据库小记 两个不同的类相互引用的情况下头文件包含问题 ctabctrl insertitem 报Debug Assertion Failed - 转角遇到你 com基于mfc和非mfc的区别 arx 打开实体对象方式 error LNK2001: unresolved external symbol __endthreadex 最小二乘法
wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString转自由骑士大哥
转角遇到你 · 2009-02-25 · via 博客园 - 转角遇到你

wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....
#include <string>
// 使用CString必须使用MFC,并且不可包含<windows.h>
#define _AFXDLL
#include <afx.h>
using namespace std;
//----------------------------------------------------------------------------------
//将 单字节char* 转换为 宽字节 wchar*
inline wchar_t* AnsiToUnicode( const char* szStr )
{
int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
if (nLen == 0)
{
   return NULL;
}
wchar_t* pResult = new wchar_t[nLen];
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
return pResult;
}
//----------------------------------------------------------------------------------
// 将 宽字节wchar_t* 转换 单字节char*
inline char* UnicodeToAnsi( const wchar_t* szStr )
{
int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
{
   return NULL;
}
char* pResult = new char[nLen];
WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
return pResult;
}
//----------------------------------------------------------------------------------
// 将单字符 string 转换为宽字符 wstring
inline void Ascii2WideString( const std::string& szStr, std::wstring& wszStr )
{
int nLength = MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, NULL, NULL );
wszStr.resize(nLength);
LPWSTR lpwszStr = new wchar_t[nLength];
MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, lpwszStr, nLength );
wszStr = lpwszStr;
delete [] lpwszStr;
}
//----------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
char*   pChar = "我喜欢char";
wchar_t* pWideChar = L"我讨厌wchar_t";
wchar_t   tagWideCharList[100] ;
char   ch = 'A';
char   tagChar[100] = {NULL};
CString   cStr;
std::string str;

// 注:设置语言环境以便输出WideChar
setlocale(LC_ALL,"chs");

// 注: char* 转换 wchar_t*
// 注: wchar_t 未重载 << ,所以不可使用 cout << 输出
pWideChar = AnsiToUnicode( pChar );
// 注:printf("%ls") 和 wprintf(L"%s") 一致
printf( "%ls\n", pWideChar );

// 注:wchar_t* 转换 wchar_t[]
wcscpy ( tagWideCharList, pWideChar );
wprintf( L"%s\n", tagWideCharList );

// 注:wchar_t[] 转换 wchar_t*
pWideChar = tagWideCharList;
wprintf( L"%s\n", pWideChar );

// 注:char 转换 string
str.insert( str.begin(), ch );
cout << str << endl;

// 注:wchar_t* 转换 string
pWideChar = new wchar_t[str.length()];
swprintf( pWideChar, L"%s", str.c_str());
wprintf( L"%s\n", pWideChar );

// 注:string 转换 char*
pChar = const_cast<char*>(str.c_str());
cout << pChar << endl;

// 注:char* 转换 string
str = std::string(pChar);
// 注: cout 的 << 重载了string, 若printf 的话必须 printf("%s", str.c_str());
//   而不可 print( "%s", str ); 因为 str 是个 string 类
cout << str << endl;

// 注:string 转换 char[]
str = "无聊啊无聊";
strcpy( tagChar, str.c_str() );
printf( "%s\n", tagChar );

// 注:string 转换 CString;
cStr = str.c_str();

// 注:CString 转换 string
str = string(cStr.GetBuffer(cStr.GetLength()));

// 注:char* 转换 CString
cStr = pChar;

// 注:CString 转换 char*
pChar = cStr.GetBuffer( cStr.GetLength() );

// 注:CString 转换 char[]
strncpy( tagChar, (LPCTSTR)CString, sizeof(tagChar));

// 注:CString 转换 wchar_t*
pWideChar = cStr.AllocSysString();
printf( "%ls\n", pWideChar );
}