MFC中char*,string和CString之间的转换
一、 将CString类转换成char*(LPSTR)类型
方法一,使用强制转换。例如:
CString theString( "This is a test" );
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;
方法二,使用strcpy。例如:
CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);
方法三,使用CString::GetBuffer。例如:
CString s(_T("This is a test "));
LPTSTR p = s.GetBuffer();
// 在这里添加使用p的代码
if(p != NULL) *p = _T('\0');
s.ReleaseBuffer();
// 使用完后及时释放,以便能使用其它的CString成员函数
CString str = "ABCDEF";
char *pBuf = str,GetBuffer( 0 );
str.ReleaseBuffer();
二、 string转char*
string 是c++标准库里面其中一个,封装了对字符串的操作
把string转换为char* 有3种方法:
1。data(),返回没有”\0“的字符串数组
如:
string str="abc";
char *p=str.data();
2.c_str 返回有”\0“的字符串数组
如:string str="gdfd";
char *p=str.c_str();
3 copy
比如
string str="hello";
char p[40];
str.copy(p,5,0); //这里5,代表复制几个字符,0代表复制的位置
*(p+5)='\0'; //要手动加上结束符
cout < < p;
三、 字符串string转换为其它数据类型
temp="123456";
1)短整型(int)
i = atoi(temp);
2)长整型(long)
l = atol(temp);
3)浮点(double)
d = atof(temp);
string s; d= atof(s.c_str());
4)BSTR变量
BSTR bstrValue = ::SysAllocString(L"程序员");
...///完成对bstrValue的使用
SysFreeString(bstrValue);
5)CComBSTR变量
CComBSTR类型变量可以直接赋值
CComBSTR bstrVar1("test");
CComBSTR bstrVar2(temp);
6)_bstr_t变量
_bstr_t类型的变量可以直接赋值
_bstr_t bstrVar1("test");
_bstr_t bstrVar2(temp);
四、 Char*转换为string
如果要把一个char 转换成string, 可以使用 string s(char *);
五、string 转CString
CString.format("%s", string.c_str());
六、char 转CString
CString.format("%s", char*);
七、 CString -> string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
八、CString互转int
将字符转换为整数,可以使用atoi、_atoi64或atol。
而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;
int i = 64;
s.Format("%d", i)
MFC中char*,string和CString之间的转换的更多相关文章
- C# 中List<T>与DataSet之间的转换
p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...
- string和数值之间的转换
string和数值之间的转换 to_string(val) 一组重载函数,返回数值val的string表示val可以是任何算数类型. stoi(s,p,b),stol(s,p,b),stoul(s,p ...
- String与StringBuffer之间的转换
来源:http://www.oschina.net/code/snippet_2261089_47352 package demo; /* String与StringBuffer之间的转换 * Str ...
- 【转】Android中dip(dp)与px之间单位转换
Android中dip(dp)与px之间单位转换 dp这个单位可能对web开发的人比较陌生,因为一般都是使用px(像素)但是,现在在开始android应用和游戏后,基本上都转换成用dp作用为单位了,因 ...
- 【Java】【9】String Date Calendar之间的转换
前言: 1, Calendar 转化 String 2, Calendar 转化 Date 3,Date 转化 String 4,Date 转化 Calendar 5,String 转化 Calend ...
- [java]转:String Date Calendar之间的转换
String Date Calendar之间的转换 String Date Calendar 1.Calendar 转化 String Calendar calendat = Calendar.ge ...
- list,string,tuple,dictionary之间的转换
list,string,tuple,dictionary之间的转换 类型 String List tuple dictionary String - list(str), str.split() tu ...
- shell 脚本文件十六进制转化为ascii码代码, Shell中ASCII值和字符之间的转换
Shell中ASCII值和字符之间的转换 1.ASCII值转换为字符 方法一: i=97 echo $i | awk '{printf("%c", $1)}' ...
- C++ 中 int,char*,string,CString之间相互转换-整理
<多字符集下> #include <string> //使用C++标准库的string类时, 定义时 std::string str; using namespace std; ...
随机推荐
- 02_c3p0之c3p0-config.xml配置案例,操作c3p0的jdbcUtil工具类的编写
c3p0也是一个开源jdbc连接池,我们熟悉的Hibernate和Spring框架使用的都是该数据源. 这里获得数据源使用的方法是:ComboPooledDataSource 它提供的构造方法有 ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- 根据iOS 10 的新特性,创建iMessage App,可用于自定义表情
第一. 介绍(原文作者 澳大利亚19岁少年--Davis Allie ----原文地址) 随着iOS10的发布,苹果对开发者开放了Messages应用程序,开发人员现在可以创建他们自己的各种类型 并且 ...
- (一〇九)UIButton的使用技巧 -imageView、titleLabel、圆角等
UIButton是一个常用控件,使用方法十分基本,但是有很多技巧常常不被注意,本文主要介绍UIButton的一些较高级技巧,用于实现图片和标签显示的美观性等. 开发时常常碰到按钮的下侧或者右侧有标题的 ...
- 【一天一道LeetCode】#160. Intersection of Two Linked Lists
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 纯CSS箭头,气泡
原文地址: CSS Triangles 演示地址:CSS Triangles Demo 原文日期: 2013年8月5日 翻译日期: 2013年8月9日 本文两种实现方式: 使用或不使用 before ...
- (六十八)使用XMPPFramework登录
按照XMPPFramework的官方样例,应该把登录代码放置在AppDelegate中,并且让注销成为私有方法. XMPPFramework进行登录的步骤如下: ①连接主机,并且发送JID ②如果连接 ...
- 【一天一道LeetCode】#82. Remove Duplicates from Sorted List II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(一)
这是一个简单的机器人投射游戏,主要来熟悉物理引擎的一些东西.你可以把它认为是机器人投篮;尽管投出的是抛物线,但不是篮球而是子弹,速度也较快. 游戏玩法是玩家选择机器人,移动机器人手臂瞄准篮框,然后发射 ...
- Android官方技术文档翻译——构建工作流
本文译自androd官方技术文档<Build Workflow>,原文地址:http://tools.android.com/tech-docs/new-build-system/buil ...