类型转换

string Int2Str(int val)  
{  
	char buf[64] = "";  
	_snprintf(buf, sizeof(buf)-1, "%d", val );  
	return buf;  
} 
int str2int( const string &val )  
{  
    return atoi(val.c_str());  
}

不能将参数 6 从“LPCSTR”转换为“LPCWSTR”

 static const char *  = 


1.dynamic_cast使用

dynamic_cast < type-id > ( expression )
该运算符把expression转换成type-id类型的对象。Type-id必须是类的指针、类的引用或者void *;
如果type-id是类指针类型,那么expression也必须是一个指针,如果type-id是一个引用,那么expression也必须是一个引用。
dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。
在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;
在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。
例如:

class CPet
{
	public:
	const std::string Compress();    
};
for(iterator iter = Begin(); iter != End(); ++iter)
{
	db::CPet *pPet = dynamic_cast<db::CPet*>(iter->second);
	pPet->Compress();
}

2.string to int

#include <iostream>
using namespace std;
void main()
{
	string stc="123";
	int test1;
	sscanf(stc.c_str(),"%d", &test1);
	if(test1==123)
		cout<<test1<<endl;
	system("pause");
} 

#include <sstream>
#include <string>
using namespace std;
void main()
{	
	std::stringstream m_strStream;
	string result = "10000";
	int n = 0;
	m_strStream<<result;
	m_strStream>>n;  //n等于10000
	if(n == 10000)
	{
		printf("%d\n",n);
	}
	system("pause");
}

3.int to string

#include <iostream>
using namespace std;

char* Int2String(int nData)
{
	char szBuffer[1024] = {0};
	sprintf(szBuffer,"%d",nData );
	return szBuffer;
}
void main()
{
	system("pause");
} 

4.枚举可以直接赋值给int

#include <iostream>
using namespace std;
void main()
{
	enum
	{
		test1,
		test2,
		test3,
		test4
	};
	int i=test4;
	cout<<i<<endl;
	system("pause");
} 

5.string to char*

#include <string>

int main()
{
	std::string s("foo");
	const char* p = s.c_str();
	char* q = const_cast<char*>(p);
}

6.char * to string

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

/*
string &assign(const char *s);用c类型字符串s赋值
string &assign(const char *s,int n);用c字符串s开始的n个字符赋值
*/

int main()
{
	char CWord[] = ", ,this, ,is a test test hao are you ,";
	string str1,str2,str3;
	str1.assign(CWord);
	str2.assign(CWord,5);
	str3.assign(CWord+5,5);
	cout<<str1<<endl;
	cout<<str2<<endl;
	cout<<str3<<endl;
	getchar();
	return 0;
}

7.int to char* and char* to int

#include   <string>
#include   <iostream>
#include	<stdlib.h>
using namespace std;
/*
string &assign(const char *s);用c类型字符串s赋值
string &assign(const char *s,int n);用c字符串s开始的n个字符赋值
*/
int main()
{
	//方法一(int to char* )
	int i=5;
	char num[16];
	memset(num,'\0',sizeof(num));
	sprintf(num,  "%d",i);
	printf("%s\n",num);	//5

	// 方法二(int to char* )
	num[0] = '\0';
	itoa(i , num, 10);	//按十进制转换 int to char *
	printf("%s",num);	//5
	i = 0;
	i = atoi(num);		//char * to int 
	printf("%d",i);		//5

	system("pause");
	return 0;
}

8.char to int

#include <iostream>
using namespace std;
int main()
{
	int a=1;
	char b = (char)(a+48);
	int c = (int)(b-48);
	if(b == '1')
		cout<<b<<endl;
	if(c == 1)
		cout<<c<<endl;
	system("pause");
}

关于强制类型转换的问题,很多书都讨论过,写的最详细的是C++ 之父的《C++ 的设计和演化》。最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_cast, dynamic_cast。标准C++中有四个类型转换符:static_cast、dynamic_cast、reinterpret_cast、和const_cast。下面对它们一一进行介绍。
static_cast
用法:static_cast < type-id > ( expression )
该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法:
用于类层次结构中基类和子类之间指针或引用的转换。进行上行转换(把子类的指针或引用转换成基类表示)是安全的;进行下行转换(把基类指针或引用转换成子类表示)时,由于没有动态类型检查,所以是不安全的。
用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。
把空指针转换成目标类型的空指针。
把任何类型的表达式转换成void类型。
注意:static_cast不能转换掉expression的const、volitale、或者__unaligned属性。
dynamic_cast
用法:dynamic_cast < type-id > ( expression )
该运算符把expression转换成type-id类型的对象。Type-id必须是类的指针、类的引用或者void *;如果type-id是类指针类型,那么expression也必须是一个指针,如果type-id是一个引用,那么expression也必须是一个引用。
dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。
在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。
class B{
public:
int m_iNum;
virtual void foo();
};
class D:public B{
public:
char *m_szName[100];
};
void func(B *pb){
D *pd1 = static_cast<D *>(pb);
D *pd2 = dynamic_cast<D *>(pb);
}
在上面的代码段中,如果pb指向一个D类型的对象,pd1和pd2是一样的,并且对这两个指针执行D类型的任何操作都是安全的;但是,如果pb指向的是一个B类型的对象,那么pd1将是一个指向该对象的指针,对它进行D类型的操作将是不安全的(如访问m_szName),而pd2将是一个空指针。另外要注意:B要有虚函数,否则会编译出错;static_cast则没有这个限制。这是由于运行时类型检查需要运行时类型信息,而这个信息存储在类的虚函数表(关于虚函数表的概念,详细可见<Inside c++ object model>)中,只有定义了虚函数的类才有虚函数表,没有定义虚函数的类是没有虚函数表的。
另外,dynamic_cast还支持交叉转换(cross cast)。如下代码所示。
class A{
public:
int m_iNum;
virtual void f(){}
};
class B:public A{
};
class D:public A{
};
void foo(){
B *pb = new B;
pb->m_iNum = 100;
D *pd1 = static_cast<D *>(pb); //copile error
D *pd2 = dynamic_cast<D *>(pb); //pd2 is NULL
delete pb;
}
在函数foo中,使用static_cast进行转换是不被允许的,将在编译时出错;而使用 dynamic_cast的转换则是允许的,结果是空指针。
reinpreter_cast
用法:reinpreter_cast<type-id> (expression)
type-id必须是一个指针、引用、算术类型、函数指针或者成员指针。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原先的指针值)。
该运算符的用法比较多。
const_cast
用法:const_cast<type_id> (expression)
该运算符用来修改类型的const或volatile属性。除了const 或volatile修饰之外, type_id和expression的类型是一样的。
常量指针被转化成非常量指针,并且仍然指向原来的对象;常量引用被转换成非常量引用,并且仍然指向原来的对象;常量对象被转换成非常量对象。
Voiatile和const类试。举如下一例:
class B{
public:
int m_iNum;
}
void foo(){
const B b1;
b1.m_iNum = 100; //comile error
B b2 = const_cast<B>(b1);
b2. m_iNum = 200; //fine
}
上面的代码编译时会报错,因为b1是一个常量对象,不能对它进行改变;使用const_cast把它转换成一个常量对象,就可以对它的数据成员任意改变。注意:b1和b2是两个不同的对象。



int to  const char*

int nModelId = 0; 

const char *  dataPtr = (const char*)&nModelId;

__int64及_atoi64使用

将字符串转换成整数时,一般会用_ttoi()或_atoi(),但有时字符串转换后的整数比较大时,用int来保存结果可能就不够了。

这时,我们得用_atoi64()来转换,而且保存结果的数据类型要用__int64。看下面的例子:

#include <iostream.h>
#include <afx.h>
 
typedef struct _tagLongLong
{
    UINT n1;
    UINT n2;
}longlong;
 
int main()
{
    CString s = "20110215160215";   // 时间:2011-2-15 16:02:15
    __int64 n = _atoi64(s);
    printf("%I64d\n", n);
//  cout<<n<<endl;                  // error C2593: 'operator <<' is ambiguous
     
    return 0;
}
通过上面的_atoi64可以保存结果,但是用cout输出却无法输出(编译错误,因为没有重载<<用来输出__int64的)。用printf可以正确输出__int64类型的数据。

原文地址:https://www.cnblogs.com/byfei/p/14104750.html