wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....转换
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 );
}
wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....转换相关推荐
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system(command)...
转载:http://blog.csdn.net/chocolateconanlan/article/details/4058868 wchar_t*,wchar_t,wchat_t数组,char,ch ...
- [转]wchar_t char std::string std::wstring CString 转换
[转]wchar_t char std::string std::wstring CString 转换 1 #include <string> 2 // 使用CString必须使用MFC, ...
- C++中std::string与C-String字符数组的互相转换
C语言中只有字符数组这一说法,没有C++专门封装的字符串类std::string.而字符数组C-String以\0作为结束符.std::string其实还是存储了C-String这个指针,只不过不同的 ...
- cstring转为string vc_VC之CString,wchar_t,int,string,char*之间的转换-阿里云开发者社区
VC之CString,wchar_t,int,string,char*之间的转换 1. CString 转 wchar_t CString path = "asdf"; wchar ...
- python 调用C++,传递int,char,char*,数组和多维数组
python 调用C++,传递int,char,char*,数组和多维数组 2018年03月21日 11:19:01
- PInvoke复习之深入理解char*与wchar_t*与string以及wstring之间的相互转换
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下 #ifndef USE_H_ #define USE_H_ #i ...
- char二维数组_C语言指针与数组详解
指针与数组 指针可以与变量结合,也可以与数组结合使用.指针数组和数组指针是两个截然不同的概念,指针数组是一种数组,该数组存放的是一组变量的地址.数组指针是一个指针,表示该指针是指向数组的指针. 1.指 ...
- c int转char数组_C语言 指向数组和字符串的指针
实例1 我们在pointer_test.c的文件中写一个test2()函数,我们定义一个有3个元素的字符数组初始化值分别为,'A', 'B', 'C',然后定义一个字符指针pc,把数组ca的首地址复制 ...
- [转]char * 和字符数组
[转]char * 和字符数组 原文地址:http://www.cnblogs.com/jeakon/archive/2012/05/27/2816809.html 代码中的int * i就是我们关注 ...
最新文章
- C++中相对路径和绝对路径
- lua学习笔记之元表和元方法
- php session 跨子域和跨服务器解决方式
- mysql 相关子查询使用【主表得数据需要扩展(统计数据依赖与其他表,但是与主表有关联)】...
- Spring事件的观察者模式
- dz自动开起html,discuz论坛开启markdown 允许html代码嵌入js
- NYOJ995硬币找零(简单dp)
- Log4j 配置的webAppRootKey参数问题
- ERROR 1820: You must reset your password using ALTER USER statement before executing this statement
- 从0到1 CTFer成功之路》任意文件读取漏洞---学习笔记
- 微信小程序弹出框详解
- 联想G480安装CentOS有线驱动
- 工程项目管理组织机构形式
- SSM-Mybatis的Mapper.xml配置文件
- android wight
- dedecms如何在首页调用会员信息(会员头像和名字等)
- CentOS + Mongodb 搭建NodeBB [转载翻译]
- python兔子和獾_Pygame-依葫芦画瓢之兔獾大战
- Niagara模块微信公众号连接
- 一键分享到新浪微博、腾讯微博、搜狐微博、人人网、开心网、百度收藏等js代码大全...
热门文章
- python下载大文件-golang/python 下载大文件时怎样避免oom
- 电脑上如何下载python-如何下载python并正确安装
- python的用途实例-python assert的用处示例详解
- 在python中、下列代码的输出是什么-python期末考试试题汇总
- python opencv 保存摄像头视频,以及fourc编码的介绍
- LeetCode 17. Letter Combinations of a Phone Number
- 微信公众平台开发 OAuth2.0网页授权认证
- HDU2015 偶数求和
- PHP与base64
- python libnum库安装使用方法