VC强制类型转换

以下是一些VC下的强制类型转换的函数

一、字符串转换
1.CString 转化为整形Int
CString str=_T("12345");
atoi((LPCSTR)str);//用LPCSTR转化为const char*即可

CString str="1";
int n=atoi(str.GetBuffer(0));

2.将一个CString转到一个char的数组(char*)中
char buffer[128]
CString str;
a.//strcpy方法
strcpy(buffer,str.GetBuffer());
str.ReleaseBuffer();
b.//强制转化方法
buffer=(LPTSTR)(LPCTSTR)str;
c.//sprintf方法
sprintf(buffer,"%s",str);

d.
CString str;
int nLength=str.GetLength();
char * sz=new char[nLength];
sz=str.GetBuffer(0);
(LPCSTR)CString

e.经过类型强制转换,可以将CString类型转换成char*,例如:
CString cStr = "Hello,world!";
char* zStr = (char*)(LPCTSTR)cStr;

3.int 转 cstring
CString string;
int iValue=100;
string.Format(_T("%d"),iValue);
MessageBox(string);
string的值为"100"

4.char * 转 CString
a.
char sz[128];
CString str;
str.Format("%s",sz);

b.
CString.format("%s", char*);
CString strtest;
char * charpoint;
charpoint="give string a value";
strtest=charpoint; //直接付值

c.
char*类型可以直接给CString,完成自动转换,例如:
char* zStr = "Hello,world!";
CString cStr = zStr;

5.Float 转 CString
float f=0.0;
CString str;
str.Format("%f",f);

6.CString 转 Float
CString str="0.0";
float f=atof(str.GetBuffer(0));

7.string 转 CString
CString.format("%s", string.c_str());
用c_str()确实比data()要好.

8.char* 转 int
#include
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);

9.CString 转 string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.

10.int 转 char *
在stdlib.h中有个函数itoa()
itoa的用法:
itoa(i,num,10);
i 需要转换成字符的数字
num 转换后保存字符的变量
10 转换数字的基数(进制)10就是说按照10进制转换数字。还可以是2,8,16等等你喜欢的进制类型
原形:char *itoa(int value, char* string, int radix);
实例:
#include "stdlib.h"
#include "stdio.h"
main()
{
int i=1234;
char s[5];
itoa(i,s,10);
printf("%s",s);
getchar();
}

11.string 转 char *
char *p = string.c_str();
string aa("aaa");
char *c=aa.c_str();

string mngName;
char t[200];
memset(t,0,200);
strcpy(t,mngName.c_str());

12、CString to LPCSTR
将CString转换成LPCSTR,需要获得CString的长度,例如:
CString cStr = _T("Hello,world!");
int nLen = cStr.GetLength();
LPCSTR lpszBuf = cStr.GetBuffer(nLen);

13、CString to LPSTR
这个和第3个技巧是一样的,例如:
CString cStr = _T("Hello,world!");
int nLen = str.GetLength();
LPSTR lpszBuf = str.GetBuffer(nLen);

14、Char[] to int
将字符串类型转换成整数型,可以使用atoi函数,例如:
char c[10];
int n;
n = atoi(c);

15、Char[] to float
和第5个技巧一样,使用atof()函数可以转换成float型,例如:
char c[10];
float f;
f = atof(c);

16、Char* to int
char *str = "100";
int i;
i = atoi(str);

一、其它数据类型转换为字符串
短整型(int)
itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制
itoa(i,temp,2); ///按二进制方式转换
长整型(long)
ltoa(l,temp,10);

二、从其它包含字符串的变量中获取指向该字符串的指针
CString变量
str = "2008北京奥运";
buf = (LPSTR)(LPCTSTR)str;
BSTR类型的_variant_t变量
v1 = (_bstr_t)"程序员";
buf = _com_util::ConvertBSTRToString((_bstr_t)v1);

三、字符串转换为其它数据类型
strcpy(temp,"123");

短整型(int)
i = atoi(temp);
长整型(long)
l = atol(temp);
浮点(double)
d = atof(temp);

四、其它数据类型转换到CString
使用CString的成员函数Format来转换,例如:

整数(int)
str.Format("%d",i);
浮点数(float)
str.Format("%f",i);
字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值
str = username;

五、BSTR、_bstr_t与CComBSTR

CComBSTR、_bstr_t是对BSTR的封装,BSTR是指向字符串的32位指针。
char *转换到BSTR可以这样: BSTR b=_com_util::ConvertStringToBSTR("数据");///使用前需要加上头文件comutil.h
反之可以使用char *p=_com_util::ConvertBSTRToString(b);

六、VARIANT 、_variant_t 与 COleVariant
VARIANT的结构可以参考头文件VC98\Include\OAIDL.H中关于结构体tagVARIANT的定义。
对于VARIANT变量的赋值:首先给vt成员赋值,指明数据类型,再对联合结构中相同数据类型的变量赋值,举个例子:
VARIANT va;
int a=2001;
va.vt=VT_I4;///指明整型数据
va.lVal=a; ///赋值

对于不马上赋值的VARIANT,最好先用Void VariantInit(VARIANTARG FAR* pvarg);进行初始化,其本质是将vt设置为VT_EMPTY,下表我们列举vt与常用数据的对应关系:

unsigned char bVal; VT_UI1
short iVal; VT_I2
long lVal; VT_I4
float fltVal; VT_R4
double dblVal; VT_R8
VARIANT_BOOL boolVal; VT_BOOL
SCODE scode; VT_ERROR
CY cyVal; VT_CY
DATE date; VT_DATE
BSTR bstrVal; VT_BSTR
IUnknown FAR* punkVal; VT_UNKNOWN
IDispatch FAR* pdispVal; VT_DISPATCH
SAFEARRAY FAR* parray; VT_ARRAY|*
unsigned char FAR* pbVal; VT_BYREF|VT_UI1
short FAR* piVal; VT_BYREF|VT_I2
long FAR* plVal; VT_BYREF|VT_I4
float FAR* pfltVal; VT_BYREF|VT_R4
double FAR* pdblVal; VT_BYREF|VT_R8
VARIANT_BOOL FAR* pboolVal; VT_BYREF|VT_BOOL
SCODE FAR* pscode; VT_BYREF|VT_ERROR
CY FAR* pcyVal; VT_BYREF|VT_CY
DATE FAR* pdate; VT_BYREF|VT_DATE
BSTR FAR* pbstrVal; VT_BYREF|VT_BSTR
IUnknown FAR* FAR* ppunkVal; VT_BYREF|VT_UNKNOWN
IDispatch FAR* FAR* ppdispVal; VT_BYREF|VT_DISPATCH
SAFEARRAY FAR* FAR* pparray; VT_ARRAY|*
VARIANT FAR* pvarVal; VT_BYREF|VT_VARIANT
void FAR* byref; VT_BYREF

_variant_t是VARIANT的封装类,其赋值可以使用强制类型转换,其构造函数会自动处理这些数据类型。
例如:
long l=222;
ing i=100;
_variant_t lVal(l);
lVal = (long)i;

COleVariant的使用与_variant_t的方法基本一样,请参考如下例子:
COleVariant v3 = "字符串", v4 = (long)1999;
CString str =(BSTR)v3.pbstrVal;
long i = v4.lVal;

七、其它

对消息的处理中我们经常需要将WPARAM或LPARAM等32位数据(DWORD)分解成两个16位数据(WORD),例如:
LPARAM lParam;
WORD loValue = LOWORD(lParam);///取低16位
WORD hiValue = HIWORD(lParam);///取高16位
对于16位的数据(WORD)我们可以用同样的方法分解成高低两个8位数据(BYTE),例如:
WORD wValue;
BYTE loValue = LOBYTE(wValue);///取低8位
BYTE hiValue = HIBYTE(wValue);///取高8位
后记:本文匆匆写成,错误之处在所难免,欢迎来信指正。
int ->str itoa,atoi
double- str ftoa,atof

_bstr_t,_variant_t,CString,long 等等看看下面:
我给你点详细的例子,看下面
先看懂_variant_t与_bstr_t这两个类的构造函数和 operator=
里面有重载了很多情况,
其他类型向_variant_t 赋值:
_variant_t( ) throw( );
_variant_t( const VARIANT& varSrc ) throw( _com_error );
_variant_t( const VARIANT* pVarSrc ) throw( _com_error );
_variant_t( const _variant_t& var_t_Src ) throw( _com_error );
_variant_t( VARIANT& varSrc, bool fCopy ) throw( _com_error );
_variant_t( short sSrc, VARTYPE vtSrc = VT_I2 ) throw( _com_error );
_variant_t( long lSrc, VARTYPE vtSrc = VT_I4 ) throw( _com_error );
_variant_t( float fltSrc ) throw( );
_variant_t( double dblSrc, VARTYPE vtSrc = VT_R8 ) throw( _com_error );
_variant_t( const CY& cySrc ) throw( );
_variant_t( const _bstr_t& bstrSrc ) throw( _com_error );
_variant_t( const wchar_t *wstrSrc ) throw( _com_error );
_variant_t( const char* strSrc ) throw( _com_error );
_variant_t( bool bSrc ) throw( );
_variant_t( IUnknown* pIUknownSrc, bool fAddRef = true ) throw( );
_variant_t( IDispatch* pDispSrc, bool fAddRef = true ) throw( );
_variant_t( const DECIMAL& decSrc ) throw( );
_variant_t( BYTE bSrc ) throw( );
operator=的重载形式:
_variant_t& operator=( const VARIANT& varSrc ) throw( _com_error );
_variant_t& operator=( const VARIANT* pVarSrc ) throw( _com_error );
_variant_t& operator=( const _variant_t& var_t_Src ) throw( _com_error );
_variant_t& operator=( short sSrc ) throw( _com_error );
_variant_t& operator=( long lSrc ) throw( _com_error );
_variant_t& operator=( float fltSrc ) throw( _com_error );
_variant_t& operator=( double dblSrc ) throw( _com_error );
_variant_t& operator=( const CY& cySrc ) throw( _com_error );
_variant_t& operator=( const _bstr_t& bstrSrc ) throw( _com_error );
_variant_t& operator=( const wchar_t* wstrSrc ) throw( _com_error );
_variant_t& operator=( const char* strSrc ) throw( _com_error );
_variant_t& operator=( IDispatch* pDispSrc ) throw( _com_error );
_variant_t& operator=( bool bSrc ) throw( _com_error );
_variant_t& operator=( IUnknown* pSrc ) throw( _com_error );
_variant_t& operator=( const DECIMAL& decSrc ) throw( _com_error );
_variant_t& operator=( BYTE bSrc ) throw( _com_error );

有了以上两个函数,举个例子:
double f=1.0
_variant_t v;
v=f; //是合法的看看operator=的重载形式就知道了
CString str="ddd"
_variant_t v;
v=str.AllocSysString() 或者v=(_bstr_t)(char*)str;
即可

_variant_t转换成别的形式
你首先必须确定你要转化成什么样的形式
double f;
_variant_t v
f=v.dblVal 即可或者f=(double)v;也可以

附:_variant_t的操作符
operator short( ) const throw( _com_error );
operator long( ) const throw( _com_error);
operator float( ) const throw( _com_error );
operator double( ) const throw( _com_error );
operator CY( ) const throw( _com_error );
operator bool( ) const throw( _com_error );
operator DECIMAL( ) const throw( _com_error );
operator BYTE( ) const throw( _com_error );
operator _bstr_t( ) const throw( _com_error );
operator IDispatch*( ) const throw( _com_error );
operator IUnknown*( ) const throw( _com_error );

1 string 2 CString
CString.format("%s",string.c_str());

2 CString 2 string

string str(CString.GetBuffer(str.GetLength()));

3 string 2 char *

char *p=string.c_str();

4 char * 2 string

string str(char*);

5 CString 2 char *

strcpy(char,CString,sizeof(char));

6 char * 2 CString

CString.format("%s",char*);

CString的format方法是非常好用的。string的c_str()也是非常常用的,但要注意和char *转换时,要把char定义成为const char*,这样是最安全的。

************************************************************************************************

1. char* to string
string s(char *);
注:在不是初始化的地方最好用assign().
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2. string to const char*
string a="strte";
const char* r=a.c_str();
注意是const的。还要转到char*:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.2. const char* to char*
const char* r="123";
char *p1 = new char[strlen(r)+1];
strcpy(p1,r);
附:http://hi.baidu.com/cfans/blog/item/06970ef4b671f366dcc4745d.html
这个页面是具体讲述区别的。
··············································································

·······························
3. cstring to string
vs2005 Unicode下:
CStringW str(L"test");
CStringA stra(str.GetBuffer(0));
str.ReleaseBuffer();
std::string strs (stra.GetBuffer(0));
stra.ReleaseBuffer();

非Unicode下:
CString cs("test");
std::string str=cs.getBuffer(0);
cs.ReleaseBuffer();

注:GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
++++++++++++++++++++++++++++++++++++++++++++++++++++
4. double ,int to string
#include
using namespace std;

stringstream ss;
string result;
long n=11111;
stream << n; //从long型数据输入
stream >>result; //转换为 string


===================================================

5.char* to int, double ,long

char *s; double x; int i; long l;

s = " -2309.12E-15";
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = "7.8912654773d210";
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = " -9885 pigs";
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

s = "98854 dollars";
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
------------------------------------------------------------------------------------------------
6. string to int ,long ,double
int s;
string str="123";
stringstream ss;
ss<ss>>s;//输出到int
ss.clear();


——————————————————————————————————————————
7. date to string
#include

char dateStr [9];
char timeStr [9];
_strdate( dateStr);
printf( "The current date is %s \n", dateStr);
_strtime( timeStr );
printf( "The current time is %s \n", timeStr);

--------实践证明是正确的版本--------------------------------------------------------------
#include
#include
#include

int main()
{
//Find the current time
time_t curtime = time(0);

//convert it to tm
tm now=*localtime(&curtime);

//BUFSIZ is standard macro that expands to a integer constant expression_r
//that is greater then or equal to 256. It is the size of the stream buffer
//used by setbuf()
char dest[BUFSIZ]={0};

//Format string determines the conversion specification's behaviour
const char format[]="%A, %B %d %Y. The time is %X";

//strftime - converts date and time to a string
if (strftime(dest, sizeof(dest)-1, format, &now)>0)
std::cout<else
std::cerr<<"strftime failed. Errno code: "< }

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
8.string to cstring

+++++++++++++++++++++++++++++++++++++++++++++++++++++
非Unicode下:
int 转 CString:
CString.Format("%d",int);
...............................
string 转 CString
CString.format("%s", string.c_str());
用c_str()确实比data()要好.
.......................................
char* 转 CString
CString.format("%s", char*);
CString strtest;
char * charpoint;
charpoint="give string a value";
strtest=charpoint; //直接付值
.....................................................
CString 转 int
CString ss="1212.12";
int temp=atoi(ss); //atoi _atoi64或atol
...................................................................................................................................
9.在Unicode下的CString to double
CSting sTemp("123.567");
double dTemp = _wtof(sTemp.GetString());


二、数值处理
1、浮点数取整方法
a、直接赋值给整数变量。如:
int i = 2.5; 或 i = (int)2.5; 这种方法采用的是四舍五入。
b、使 用 floor函 数 。 floor(x)返回的是x的整数部分。如:
floor(2.5) = 2
floor(-2.5) = -2
c、使用ceil函数。ceil(x)返回的是不大于x的最小整数。如 :
ceil(2.5) = 2
ceil(-2.5) = -3
d、求余数%,fmod函数。
8 % 3 = 2
fmod(8.1,3) = 2.1。适用于浮点数。

你可能感兴趣的:(VC深入浅出随堂笔记)