VC类型转换(实测)

一、字符串转换

1.CString 转 Int

CString str1 = _T("12345");
//用LPCSTR转化为const char*即可
atoi((LPCSTR)str1);

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

2.CString 转 char*

char buffer[10];
CString str("123546");

//strcpy方法
strcpy(buffer,str.GetBuffer());
str.ReleaseBuffer();

//强制转化方法
buffer = (LPTSTR)(LPCTSTR)str;

//sprintf方法
sprintf(buffer,"%s",str);

//copy 如"abc",占四个字节,strlen的值是3,加1的理由
CString theString(_T("This is a test"));
int sizeOfString = (theString.GetLength() + 1);
LPTSTR lpsz = new TCHAR[sizeOfString];
_tcscpy_s(lpsz, sizeOfString, theString);

3.CString 转 Float

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

4、CString to LPCSTR

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

5、CString to LPSTR

//同上
CString cStr = _T("Hello,world!");
int nLen = str.GetLength();
LPSTR lpszBuf = str.GetBuffer(nLen);

6.CString 转 string

string strA ;
CString cstrB = "123";
strA = cstrB.GetBuffer();
cstrB.ReleaseBuffer(); 

//releaseBuffer则是解除锁定,使得CString对象在以后的代码中继续可以实现长度自适应增长

7.char * 转 CString

//a.Format
char sz[] = "12345";
CString str;
str.Format("%s", sz);

//b.char*类型可以直接给CString
char* zStr = "Hello,world!";
CString cStr = zStr;

8.Float 转 CString

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

9.int 转 Cstring

CString string;
int iValue = 100;
string.Format(_T("%d"),iValue);
cout << string;

10.string 转 CString

//Format
string strA = "132";
CString cstrB;
cstrB.Format("%s", strA.c_str());

11.char* 转 int

#include <stdlib.h>
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);

12.int 转 char *

//在stdlib.h中有个函数itoa
 int i=1234;
 char s[5];
 itoa(i,s,10);

13.string 转 char *

//c_str函数
string aa("aaa");
char *p = (char *)aa.c_str();

//strcpy函数
char t[200];
strcpy(t, aa.c_str());

14、Char[] to int

 //atoi函数
 char c[] = "123";
 int n;
 n = atoi(c);

15、Char[] to float

//atoi函数
 char c[10] = "123";
 float f;
 f = atof(c);

16、Char* to int

//atoi函数
 char *str = "100";
 int i;
 i = atoi(str);

二、其它转换

1.BSTR、_bstr_t与CComBSTR

CComBSTR、_bstr_t是对BSTR的封装,BSTR是指向字符串的32位指针。

//char *转换BSTR可,使用前需要加上头文件comutil.h 
//Configuration Properties-> C/C++->Language
Treat wchar_t as Built-in Type = Yes

#pragma comment(lib,"comsuppw.lib")
BSTR b =_com_util::ConvertStringToBSTR("数据");
char *p = _com_util::ConvertBSTRToString(b);

2、VARIANT 、_variant_t 与 COleVariant。
对于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;
int i = 100;
_variant_t lVal(l);
lVal = (long)i;

COleVariant的使用与_variant_t的方法基本一样,请参考如下例子:

COleVariant v3 = _T("字符串"),v4 = (long)1999;
CString str = (BSTR)v3.pbstrVal;
long i = v4.lVal;

// =操作符重载 和 构造函数
有了以上两个函数,举个例子:

double f = 1.0;
_variant_t v;
v = f; //是合法的看看operator=的重载形式就知道了
CString str="ddd";
_variant_t v;
v = str.AllocSysString(); 
//  v = (_bstr_t)(LPSTR)(LPCTSTR)str;

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

附:_variant_t的操作符

operator short( )   Extracts a short integer value. 

operator long( )   Extracts a long integer value. 

operator float( )   Extracts a float numerical value. 

operator double( )   Extracts a double integer value. 

operator CY( )   Extracts a CY object. 

operator bool( )   Extracts a bool value. 

operator DECIMAL( )   Extracts a DECIMAL value. 

operator BYTE( )   Extracts a BYTE value. 

operator _bstr_t( )   Extracts a string, which is encapsulated in a _bstr_t object. 

operator IDispatch*( )   Extracts a dispinterface pointer from an encapsulated VARIANT. AddRef is called on the resulting pointer, so it is up to you to call Release to free it. 

operator IUnknown*( )   Extracts a COM interface pointer from an encapsulated VARIANT. AddRef is called on the resulting pointer, so it is up to you to call Release to free it. 

三、其它常用

1.对消息的处理
将WPARAM或LPARAM等32位数据(DWORD)分解成两个16位数据(WORD),

LPARAM lParam;
WORD loValue = LOWORD(lParam);//取低16位
WORD hiValue = HIWORD(lParam);//取高16位6位的数据(WORD)

我们可以用同样的方法分解成高低两个8位数据(BYTE),例如:

WORD wValue;
BYTE loValue = LOBYTE(wValue);///取低8位
BYTE hiValue = HIBYTE(wValue);///取高8位
  1. date to string
#include <time.h>
using namespace std;
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 <iostream>
#include <ctime>
#include <cerrno>

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<<dest<<std::endl;
 else
 std::cerr<<"strftime failed. Errno code: "<<errno<<std::endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骇客之技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值