关闭

类型转换小结

标签: C++字符串变量类型转换编程数值转换
18123人阅读 评论(0) 收藏 举报
分类:

在编程的过程中,经常遇到变量类型转换问题,在此小结一下:

以下程序的头文件

#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

(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; 
 }







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:598471次
    • 积分:2246
    • 等级:
    • 排名:第18399名
    • 原创:22篇
    • 转载:1篇
    • 译文:0篇
    • 评论:8条
    最新评论