c++中的一些类型转换 : CString,string,int ,long,double,char*,const char*,date等----留着备用
来源:互联网 发布:网络传输介质 编辑:程序博客网 时间:2024/05/23 13:46
c++中的一些类型转换 : CString,string,int ,long,double,char*,const char*,date等
1. char* to string
string s(char *);
注:在不是初始化的地方最好用assign().
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2. string to const char*
string a="strte";
const char* r=a.c_str();
注意是const的。还要转到char*:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.2. const char* to char*
const char* r="123";
char *p1 = new char[strlen(r)+1];
strcpy(p1,r);
附:http://hi.baidu.com/cfans/blog/item/06970ef4b671f366dcc4745d.html
这个页面是具体讲述区别的。
·············································································································
3. cstring to string
vs2005 Unicode下:
CStringW str(L"test");
CStringA stra(str.GetBuffer(0));
str.ReleaseBuffer();
std::string strs (stra.GetBuffer(0));
stra.ReleaseBuffer();
非Unicode下:
CString cs("test");
std::string str=cs.getBuffer(0);
cs.ReleaseBuffer();
注:GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
++++++++++++++++++++++++++++++++++++++++++++++++++++
4. double ,int to string
#include <sstream>
using namespace std;
stringstream ss;
string result;
long n=11111;
stream << n; //从long型数据输入
stream >>result; //转换为 string
===================================================
5.char* to int, double ,long
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s/t/tinteger: %d/n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s/t/tlong: %ld/n", s, l );
------------------------------------------------------------------------------------------------
6. string to int ,long ,double
int s;
string str="123";
stringstream ss;
ss<<str;//从str输入
ss>>s;//输出到int
ss.clear();
——————————————————————————————————————————
7. date to string
#include <time>
using namespace std;
char dateStr [9];
char timeStr [9];
_strdate( dateStr);
printf( "The current date is %s /n", dateStr);
_strtime( timeStr );
printf( "The current time is %s /n", timeStr);
--------实践证明是正确的版本--------------------------------------------------------------
#include <iostream>
#include <ctime>
#include <cerrno>
int main()
{
//Find the current time
time_t curtime = time(0);
//convert it to tm
tm now=*localtime(&curtime);
//BUFSIZ is standard macro that expands to a integer constant expression
//that is greater then or equal to 256. It is the size of the stream buffer
//used by setbuf()
char dest[BUFSIZ]={0};
//Format string determines the conversion specification's behaviour
const char format[]="%A, %B %d %Y. The time is %X";
//strftime - converts date and time to a string
if (strftime(dest, sizeof(dest)-1, format, &now)>0)
std::cout<<dest<<std::endl;
else
std::cerr<<"strftime failed. Errno code: "<<errno<<std::endl;
}
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
8.string to cstring
+++++++++++++++++++++++++++++++++++++++++++++++++++++
非Unicode下:
int 转 CString:
CString.Format("%d",int);
...............................
string 转 CString
CString.format("%s", string.c_str());
用c_str()确实比data()要好.
.......................................
char* 转 CString
CString.format("%s", char*);
CString strtest;
char * charpoint;
charpoint="give string a value";
strtest=charpoint; //直接付值
.....................................................
CString 转 int
CString ss="1212.12";
int temp=atoi(ss); //atoi _atoi64或atol
...................................................................................................................................
9.在Unicode下的CString to double
CSting sTemp("123.567");
double dTemp = _wtof(sTemp.GetString());
- c++中的一些类型转换 : CString,string,int ,long,double,char*,const char*,date等----留着备用
- c++中的一些类型转换 : CString,string,int ,long,double,char*,const char*,date等
- c++中的一些类型转换 : CString,string,int ,long,double,char*,const char*,date等
- c++中的一些类型转换 : CString,string,int ,long,double,char*,const char*,date等
- c++中的一些类型转换 : CString,string,int ,long,double,char*,const char*,date等 .
- CString,string,int ,long,double,char*,const char*,date等----留着备用
- CString,string,int ,long,double,char*,const char*,date等----留着备用
- c++中的一些类型转换 : CString,string,int ,long,double,char*,const char*,date
- long、string和CString类型转换,CString转double、char *
- C++,CString,string,char*,int等转换
- java中String,int,Integer,char,Long,Float,Double,Date 类型转换
- String数据类型转换成long、int、double、float、boolean、char等七种数据类型
- vs2010 unicode 下类型转换CString,char*,char,string,int,double
- 类型转换string,char,int,double
- VC中int,char,char*,const char*,string等类型转换
- VC中int,char,char*,const char*,string等类型转换
- VC中int,char,char*,const char*,string等类型转换(转载)
- CString,string,char*,int类型转换
- hdu 1009 FatMouse' Trade
- ORA-31693, ORA-02354 and ORA-01555 with Export Datapump
- lua学习笔记--交互模式简单说明
- 基于mac平台上用cocos2dx开发屏幕竖屏设置 x-code5
- 几种索引扫描方式学习
- c++中的一些类型转换 : CString,string,int ,long,double,char*,const char*,date等----留着备用
- ios面试
- vc 线程创建关闭(彻底讲解)
- iOS网络编程实践--NSStream实现TCP Socket iPhone客户端
- 比较两个时间大小的三种方法
- initrd映像文档的作用和制作
- live555源码分析----H264的数据处理
- 8.12外汇及贵金属日分析报告
- c++ windows error C2662 C2663