C++ 类型转换 atoi atol atof <stdlib.h>, itoa ftoa char <---> string
来源:互联网 发布:都玩网络手游 编辑:程序博客网 时间:2024/06/05 02:37
int atoi ( const char * str );
Convert string to integer
Parses the C string str interpreting its content as an integral number, which is returned as anint value./* atoi example */#include <stdio.h>#include <stdlib.h>int main (){ int i; char szInput [256]; printf ("Enter a number: "); fgets ( szInput, 256, stdin ); i = atoi (szInput); printf ("The value entered is %d. The double is %d.\n",i,i*2); return 0;}
outputEnter a number: 73The value entered is 73. The double is 146.
long int atol ( const char * str );
Convert string to long integer
Parses the C string str interpreting its content as an integral number, which is returned as along int value./* atol example */#include <stdio.h>#include <stdlib.h>int main (){ long int li; char szInput [256]; printf ("Enter a long number: "); gets ( szInput ); li = atol (szInput); printf ("The value entered is %d. The double is %d.\n",li,li*2); return 0;}
outputEnter a number: 567283The value entered is 567283. The double is 1134566.
double atof ( const char * str );
Convert string to double
Parses the C string str interpreting its content as a floating point number and returns its value as adouble./* atof example: sine calculator */#include <stdio.h>#include <stdlib.h>#include <math.h>int main (){ double n,m; double pi=3.1415926535; char szInput [256]; printf ( "Enter degrees: " ); gets ( szInput ); n = atof ( szInput ); m = sin (n*pi/180); printf ( "The sine of %f degrees is %f\n" , n, m ); return 0;}Output
Enter degrees: 45The sine of 45.000000 degrees is 0.707101
char * itoa ( int value, char * str, int base );
Convert integer to string (non-standard function)
Converts an integer value to a null-terminated string using the specifiedbase and stores the result in the array given by str parameter.This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.A standard-compliant alternative for some cases may be sprintf:
- sprintf(str,"%d",value) converts to decimal base.
- sprintf(str,"%x",value) converts to hexadecimal base.
- sprintf(str,"%o",value) converts to octal base
/* itoa example */#include <stdio.h>#include <stdlib.h>int main (){ int i; char buffer [33]; printf ("Enter a number: "); scanf ("%d",&i); itoa (i,buffer,10); printf ("decimal: %s\n",buffer); itoa (i,buffer,16); printf ("hexadecimal: %s\n",buffer); itoa (i,buffer,2); printf ("binary: %s\n",buffer); return 0;}
OutputEnter a number: 1750decimal: 1750hexadecimal: 6d6binary: 11011010110
#include <sstream>string convertDouble(double value) { std::ostringstream o; if (!(o << value)) return ""; return o.str();}
1. const char *c_str();c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。注意:一定要使用strcpy()等函数来操作c_str()返回的指针比如:最好不要这样:char* c;string s="1234";c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理应该这样用:char c[20];string s="1234";strcpy(c,s.c_str());这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作(只能对其拷贝)再举个例子c_str() 以 char* 形式传回 string 内含字符串如果一个函数要求char*参数,可以使用c_str()方法:string s = "Hello World!";printf("%s", s.c_str()); //输出 "Hello World!" 2. const * char c_str() 一个将string转换为 const* char的函数。string的c_str()返回的指针是由string管理的。它的生命期是string对象的生命期。然后可以按C的方式使用这个指针,或把它的内容复制出来。 例如: string s; cin>>s; const char *ch=s.c_str(); 这样就可以从标准输入里输入任意长的字符串,并按const *char来使用。3. 如果要把一个char 转换成string, 可以使用 string s(char *); 4. 其他类型转换方式:string 转 CString CString.format("%s", string.c_str()); char 转 CString CString.format("%s", char*); 备注一些:
#include <string>
using namespace std;
#include <sstream>
#include <iostream>
#include <stdlib.h>
string getstring ( const int n )
{
}
string 转 CString
CString.format(”%s”, string.c_str());
char 转 CString
CString.format(”%s”, char*);
char 转 string
string s(char *);
string 转 char *
char *p = string.c_str();
CString 转 string
string s(CString.GetBuffer());
1,string -> CString
CString.format(”%s”, string.c_str());
用c_str()确实比data()要好.
2,char -> string
string s(char *);
只能初始化,在不是初始化的地方最好还是用assign().
3,CString -> string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
《C++标准函数库》中说的
有三个函数可以将字符串的内容转换为字符数组和C—string
1.data(),返回没有”\0“的字符串数组
2,c_str(),返回有”\0“的字符串数组
3,copy()
—————————————————————
CString与int、char*、char[100]之间的转换- -
CString与int、char*、char[100]之间的转换- -
CString互转int
将字符转换为整数,可以使用atoi、_atoi64或atol。
而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;
int i = 64;
s.Format(”%d”, i)
Format函数的功能很强,值得你研究一下。
void CStrDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString
ss=”1212.12″;
int temp=atoi(ss);
CString aa;
aa.Format(”%d”,temp);
AfxMessageBox(”var is ” + aa);
}
sart.Format(”%s”,buf);
CString互转char*
///char * TO cstring
CString strtest;
char * charpoint;
charpoint=”give string a value”;
strtest=charpoint;
///cstring TO char *
charpoint=strtest.GetBuffer(strtest.GetLength());
标准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++ 类型转换 atoi atol atof <stdlib.h>, itoa ftoa char <---> string
- C++ 类型转换 atoi atol atof itoa ftoa <stdlib.h>
- c字符串转换函数atoi、atof、atol
- 标准库函数itoa,atoi,atol,atof
- itoa与atoi ftoa与atof的实现
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- 一天一个C Run-Time Library 函数 (10) atol,atof,atoi, ltoa , itoa,
- C++中,int和char互转函数_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- atoi,itoa,atof,atol,gcvt,strtod,atrtod,strtol,strtoul,toascii
- Pro Android学习笔记(四):了解Android资源(下)
- maven的中心仓库
- 后台作业触发事件定义以及事件在程序中的调用
- javascript 面向对象编程
- Delphi包BPL函数
- C++ 类型转换 atoi atol atof <stdlib.h>, itoa ftoa char <---> string
- 检测合法的伙伴系统区间
- 淘宝技术这十年(子柳著)读书笔记
- 修改状态栏的时间
- 安装redis
- MAC下显示隐藏文件
- InnoDB存储引擎后台线程与内存池
- size_type 也算unsigned int符号学习
- 用户体验时代的9条建议