C++ 中int,char,string,CString类型转换简记
来源:互联网 发布:大学专业解析知乎 编辑:程序博客网 时间:2024/05/19 19:58
1) 在C标准库里面,使用atoi:
#include <cstdlib> #include <string>std::string text = "15"; int number = std::atoi( text.c_str() ); if (errno == ERANGE) //可能是std::errno { //number可能由于过大或过小而不能完全存储 } else if (errno == ????) //可能是EINVAL { //不能转换成一个数字 }
2) 在C++标准库里面,使用stringstream:(stringstream 可以用于各种数据类型之间的转换)
#include <sstream> #include <string>std::string text = "152"; int number; std::stringstream ss;ss << text;//可以是其他数据类型ss >> number; //string -> intif (! ss.good()) { //错误}ss << number;// int->stringstring str = ss.str(); if (! ss.good()) { //错误}
3) 在Boost库里面,使用lexical_cast:
#include <boost/lexical_cast.hpp> #include <string>try { std::string text = "152"; int number = boost::lexical_cast< int >( text ); } catch( const boost::bad_lexical_cast & ) { //失败 }
2.string 转 CString
CString.format(”%s”, string.c_str());
用c_str()确实比data()要好;
3.char 转 CString
CString.format(”%s”, char*);
4.char 转 string
string s(char *);
只能初始化,在不是初始化的地方最好还是用assign().
5.string 转 char *
char *p = string.c_str();
6.CString 转 string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
7.字符串的内容转换为字符数组和C—string
(1) data(),返回没有”\0“的字符串数组
(2) c_str(),返回有”\0“的字符串数组
(3) copy()
8.CString与int、char*、char[100]之间的转换
(1) CString互转int
将字符转换为整数,可以使用atoi、_atoi64或atol。而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;int i = 64;s.Format(”%d”, i)
Format函数的功能很强,值得你研究一下。
void CStrDlg::OnButton1(){ CString ss=”1212.12″; int temp=atoi(ss); CString aa; aa.Format(”%d”,temp); AfxMessageBox(”var is ” + aa);}
(2) CString互转char*
///char * TO cstringCString strtest;char * charpoint;charpoint=”give string a value”; //?strtest=charpoint;///cstring TO char *charpoint=strtest.GetBuffer(strtest.GetLength());
(3) 标准C里没有string,char *==char []==string, 可以用CString.Format(”%s”,char *)这个方法来将char *转成CString。
要把CString转成char *,用操作符(LPCSTR)CString就可以了。
CString转换 char[100]
char a[100]; CString str(”aaaaaa”); strncpy(a,(LPCTSTR)str,sizeof(a));
- C++ 中int,char,string,CString类型转换简记
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*转换
- MFC/C++/C中字符类型CString, int, string, char*转换(转)
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- C++ 中int,char,string,CString类型转换
- C++ 中int,char,string,CString类型转换
- C++ 中int,char,string,CString类型转换
- C++ 中int,char,string,CString类型转换(一)
- C++ 中int,char,string,CString类型转换
- the vfpoledb provider is not registered on the local machine
- WTL入门(2)-- WTL基础
- 让div居中而文字不居中的例子
- 深入分析 Java 中的中文编码问题
- poj 1011/uva 307【经典dfs+超强剪枝+记忆化搜索】
- C++ 中int,char,string,CString类型转换简记
- C#获取AD用户信息
- 经典SQL自定义函数
- 解决android emulator-5554 disconnected的问题的方法
- 发现一个go语言window开发平台gomingw【心情好,散点分】
- .NET简谈设计模式之(命令模式)
- flash动态改变注册点
- 处理XML中的控制符
- 强类型ASP.NET数据绑定