C/C++データ型変換小結

5662 ワード

プログラミングの過程で、変数タイプ変換の問題によく遭遇します.ここでまとめます.
char*とstring、wstring、CStringの間の変換を含む.
char*、stringとint、float、doubleの間の変換;
以下のプログラムのヘッダファイル
#include
#include
#include 
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(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<>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*
利用関数T 2 A(CString);
char*からCString
関数A 2 T(char*);