1. 程式人生 > >C/C++資料型別轉換小結

C/C++資料型別轉換小結

在程式設計的過程中,經常遇到變數型別轉換問題,在此小結一下:

包含char*與string、wstring,CString之間的轉換;

char*、string與int、float、double之間的轉換;

以下程式的標頭檔案

#include<iostream>
#include<string>
#include<sstream> 
using namespace std;

(1)char*      char[]     到string的轉換:

//char*  to  string
int CP1()
{
	char *p = "abcd";
	char t[] = "abcde";
	string test;
	test = p;
	cout << test << endl;
	cout << test.length() << endl;

	test = t;
	cout << test << endl;
	cout << test.length() << endl;
	return 0;
}
(2)string 到 char * 的轉換:
//string  to  char*
int CP2()
{
	string test = "abc";
	char t[10];
	strcpy(t, test.c_str());
	cout << t << endl;
	cout << strlen(t) << endl;

	char *te;
	int number = test.length();
	te = new char[number + 1];
	cout << number << endl;
	/*test.copy(te, number, 0);
	*(te+number) = '0';
	cout << te << endl;*/

	strcpy(te, test.c_str());
	cout << te << endl;
	cout << strlen(te) << endl;

	delete[] te;
	return 0;		 
}
(3)string到const char* 的轉換:
//string  to  const char*
int CP3()
{
	const char* c;
	string test = "1234";
	c = test.c_str();
	cout << c << endl;
	const char* t;
	t = test.data();
	cout << t << endl;
	return 0;
}
(4)int 到 string的轉換:
//int to string
int CP4()
{
	int nt = 30;
	char c[30];

	//using C template to trans from int to string
	itoa(nt, c, 10);//引數3為進位制,以幾進位制轉換為 char
	cout << c << endl;
	cout << strlen(c) << endl;

	//using stringstream to trans from int to string
	stringstream test;
	test << nt;
	string stest = test.str();
	cout << stest << endl;
	cout << stest.length() << endl;

	//using to_string to trans from int to string  as the same double and float
	string str= to_string(nt);
	cout << str << endl;
	cout << str.length() << endl;
	return 0;
}
(5)string到int的轉換:
//string to int
int CP5()
{
	string test = "17";
	char* t;
	int nt = static_cast<int>(strtol(test.c_str(), &t, 10));//10 表示轉換進位制
	cout << nt << endl;

	int nte;
	sscanf(test.c_str(), "%d", &nte);//%d 表示十進位制
	cout << nte << endl;

	//using stringstream to trans from string to int
	stringstream stest;
	stest << test;
	int ntes;
	stest >> ntes;
	cout << ntes << endl;

	//using C template to trans from string to int
	int ntest = atoi(test.c_str());
	cout << ntest << endl;
	
	//using C++ template to trans from string to int
	int nnt;
	nnt = stoi(test.c_str());
	cout << nnt << endl;
	return 0;
}
(6)string 到 double的轉換:
//string to double
int CP6()
{
	string test = "17.101";
	//using C template to trans from doubel to string
	double t = atof(test.c_str());
	cout << t << endl;
	return 0;
}

數值型別到string的轉換可以有以下幾種方式:

(1)使用函式模板+stringstream

int型轉string型
 

void int2str(const int &int_temp,string &string_temp)  
{  
        stringstream stream;  
        stream<<int_temp;  
        string_temp=stream.str();   //此處也可以用 stream>>string_temp  
}  
 
string型轉int型
 

void str2int(int &int_temp,const string &string_temp)  
{  
    stringstream stream(string_temp);  
    stream>>int_temp;  
}  

(2)使用標準庫函式std::to_string:

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
(3)使用C標準庫函式:
atoi(str.c_str());
strtoul(str.c_str(), NULL, 10);
atof(str.c_str());
(4)使用C++標準庫函式:
stoi()

在呼叫windowAPI函式的時候經常會遇到char與wchar的問題,在此貼出兩者之間的轉換函式:

(1)string  to WString

BOOL StringToWString(const std::string &str,std::wstring &wstr)
 {    
     int nLen = (int)str.length();    
     wstr.resize(nLen,L' ');
 
     int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);
 
     if (nResult == 0)
     {
         return FALSE;
     }
 
     return TRUE;
 }
(2)WString  to   string
BOOL WStringToString(const std::wstring &wstr,std::string &str)
 {    
     int nLen = (int)wstr.length();    
     str.resize(nLen,' ');
 
     int nResult = WideCharToMultiByte(CP_ACP,0,(LPCWSTR)wstr.c_str(),nLen,(LPSTR)str.c_str(),nLen,NULL,NULL);
 
     if (nResult == 0)
     {
         return FALSE;
     }
 
     return TRUE;
 }
(3)String    to    Wstring
std::wstring StringToWString(const std::string &str)
 {
     std::wstring wstr(str.length(),L' ');
     std::copy(str.begin(), str.end(), wstr.begin());
     return wstr; 
 }
(4)  Wstring    to   string
std::string WStringToString(const std::wstring &wstr)
 {
     std::string str(wstr.length(), ' ');
     std::copy(wstr.begin(), wstr.end(), str.begin());
     return str; 
 }
CString to string 

string = CStringA(CString)

APPENDED

利用ATL進行char*與wchar*(char*與CString)之間的轉換

需要包含巨集

USES_CONVERSION; 

CString到char*

利用函式T2A(CString);

char*到CString

使用函式A2T(char*);