C++ 类型转换 atoi atol atof <stdlib.h>, itoa ftoa char <---> string

来源:互联网 发布:都玩网络手游 编辑:程序博客网 时间:2024/06/05 02:37
http://blog.163.com/chen_dawn/blog/static/1125063201011203536852/
1. atoi
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;}
output
Enter a number: 73The value entered is 73. The double is 146.


2. atol
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;}
output
Enter a number: 567283The value entered is 567283. The double is 1134566.
3. atof
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;}OutputEnter degrees: 45The sine of 45.000000 degrees is 0.707101
4. itoa
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;}
Output
Enter a number: 1750decimal: 1750hexadecimal: 6d6binary: 11011010110
5. ftoa
#include <sstream>string convertDouble(double value) {  std::ostringstream o;  if (!(o << value))    return "";  return o.str();}
6. string to char
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*);  备注一些:
【转】C++ int,char,string,CString类型转换(整理总结)

#include <string> //使用C++标准库的string类时

using namespace std; //同上

#include <sstream>

#include <iostream>

#include <stdlib.h> //要将string类和int类型直接转换最好有这些包含,

//因为自己写一个转换函数比较方便,函数定义参考如下

string getstring ( const int n )

{

std::stringstream newstr;
newstr<<n;
return newstr.str();

}

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));