cstring 转 string
都通过基本类型来转换即可:
CString可以转换为基本类型LPCTSTR,LPCTSTR根据项目编码可以是const char*或者const wchar_t*;string可以用c_str()转换为const char*,stringw可以用c_str()转换为const w_char*。而CString和string/w都重载了=赋值操作符,可以把char*或者wchar_t*字符串赋值给CString或者string/w类型的变量,或者调用其构造函数。
string to CString比较简单
string str="abcde";
CString cstr(str.c_str());
CString to string,要看你的CString用的是UNICODE还是非UNICODE,
非UNICODE就简单了
CString cs="abcde";
string str(cs.GetBuffer(cs.GetLength()));
UNICODE就麻烦点
需要转换一下才行,给个函数你用
CString cs=_T("abcde");
string str=CGeneralUtility::WChar2Ansi(cs.GetBuffer(cs.GetLength()));
string CUtility::WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen<= 0) return std::string("");
char* pszDst = new char[nLen];
if (NULL == pszDst) return std::string("");
WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen -1] = 0;
std::string strTemp(pszDst);
delete [] pszDst;
return strTemp;
}
1,string -> CString
CString.format("%s", string.c_str());
2,char -> string
string s(char *);
3,CString -> string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
cstring 转 string的更多相关文章
- CString转string
如题,找了半天... //CString转string USES_CONVERSION; CString temp; temp = _T("kjdsaflkjdlfkj"); ch ...
- CString和string
CString和string(一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: CString(typedef CS ...
- Cstring到string
要利用mfc,然后接受一个图片. imread只能读const string& filename 的东西. imread 原型: CV_EXPORTS_W Mat imread( ); 它的参 ...
- unicode下各种类型转换CString、string
把最近用到的各种unicode下类型转换总结了一下: 1.string转CString string a=”abc”; CString str=CString(a.c_str()); 或str.for ...
- unicode下各种类型转换,CString,string,char*,int,char[]
把最近用到的各种unicode下类型转换总结了一下,今后遇到其他的再补充: 1.string转CString string a=”abc”; CString str=CString(a.c_str() ...
- stringstream转换CString为string出错
使用stringstream转换CString为string时,调试时发现是CString赋给stringstream没有问题,stringstram赋给string就不行,倒也不是没有赋成功,只是赋 ...
- C++———库函数cstring及string方法解读
1.string与cstring区别 <string>是C++标准库头文件.包含了拟容器class std::string的声明(不过class string事实上只是basic_stri ...
- CString与string、char*的区别和转换
转自:http://blog.csdn.net/luoweifu/article/details/20232379 我们在C++的开发中经常会碰到string.char*以及CString,这三种都表 ...
- Cpp读文件、CString转String、String转CString
场景 C++读取文件 技术点 读取文件 fstream提供了三个类,用来实现c++对文件的操作.(文件的创建.读.写). ifstream -- 从已有的文件读入 ofstream -- 向文件写内容 ...
- 【转】CString与string、char*的区别和转换
我们在C++的开发中经常会碰到string.char*以及CString,这三种都表示字符串类型,有很多相似又不同的地方,常常让人混淆.下面详细介绍这三者的区别.联系和转换: 各自的区别 char*: ...
随机推荐
- Linux ANSI 乱码问题
原帖http://blog.chinaunix.net/u/24624/showart_184609.html Windows 到 linux 解决samba-3.x客户端中文乱码问题 dos cha ...
- java 反射和暴力反射 两个DEMO
</pre><pre code_snippet_id="402084" snippet_file_name="blog_20140622_5_93502 ...
- A Complete Guide to Usage of ‘usermod’ command– 15 Practical Examples with Screenshots
https://www.tecmint.com/usermod-command-examples/ -------------------------------------------------- ...
- UIWebView 设置背景为透明
UIWebView的背景怎样设置成为透明? [webview setBackgroundColor:[UIColor clearColor]]; [webview setOpaque:NO]; 两句代 ...
- C++学习之extern "C"
我们知道,extern关键字可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义.这里起到的是声明作用范围的用处.另外,extern还可以与 ...
- 性能测试实战-XYB项目-内网访问
使用内网服务器,linux host绑定域名,相当于ip地址+域名的host绑定,只不过这里的ip是yc-sparks.schoolis.cn
- NSDate 工具
#import "NSDate+XMGExtension.h" @implementation NSDate (XMGExtension) /** * 是否为今天 */ - (BO ...
- LeetCode 690. Employee Importance (职员的重要值)
You are given a data structure of employee information, which includes the employee's unique id, his ...
- 源代码编译安装MySQL5.6.12具体过程
1 下载安装包download tar.gzwget http://download.csdn.net/detail/mchdba/75450372 安装cmake软件包yum install cm ...
- GPS时间
GPS信息里面包含一个时间戳. phonegap(即cordova)的地理位置API Geolocation 提供了对设备GPS传感器的访问,返回的数据中,除了坐标,还有一个时间戳timestamp. ...