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 an
int 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 a
long 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 a
double.
/* 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 specified
base 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));