| C++常用数据类型转换 |
|
| 作者:babyblue… 文章来源:本站原创 点击数: 更新时间:2006-10-29 |
|
|
常用数据类型转化及操作
1.1 数学类型变量与字符串相互转换(这些函数都在STDLIB.H里)
(1)将数学类型转换为字符串可以用以下一些函数:
举例: _CRTIMP char * __cdecl _itoa(int, char *, int);//这是一个将数字转换为一个字符串类型的函数,最后一个int表示转换的进制
如以下程序:
int iTyep=3;
char *szChar;
itoa(iType,szChar,2);
cout<输出为1010
类似函数列表:
_CRTIMP char * __cdecl _itoa(int, char *, int);//为了完整性,也列在其中
_CRTIMP char * __cdecl _ultoa(unsigned long, char *, int);
_CRTIMP char * __cdecl _ltoa(long, char *, int);
_CRTIMP char * __cdecl _i64toa(__int64, char *, int);
_CRTIMP char * __cdecl _ui64toa(unsigned __int64, char *, int);
_CRTIMP wchar_t * __cdecl _i64tow(__int64, wchar_t *, int);
_CRTIMP wchar_t * __cdecl _ui64tow(unsigned __int64, wchar_t *, int);
_CRTIMP wchar_t * __cdecl _itow (int, wchar_t *, int);//转换为长字符串类型
_CRTIMP wchar_t * __cdecl _ltow (long, wchar_t *, int);
_CRTIMP wchar_t * __cdecl _ultow (unsigned long, wchar_t *, int);
还有很多,请自行研究
(2)将字符串类型转换为数学类型变量可以用以下一些函数:
举例: _CRTIMP int __cdecl atoi(const char *);//参数一看就很明了
char *szChar=”88”;
int temp(0);
temp=atoi(szChar);
cout<
类似的函数列表:
_CRTIMP int __cdecl atoi(const char *);
_CRTIMP double __cdecl atof(const char *);
_CRTIMP long __cdecl atol(const char *);
_CRTIMP long double __cdecl _atold(const char *);
_CRTIMP __int64 __cdecl _atoi64(const char *);
_CRTIMP double __cdecl strtod(const char *, char **);//
_CRTIMP long __cdecl strtol(const char *, char **, int);//
_CRTIMP long double __cdecl _strtold(const char *, char **);
_CRTIMP unsigned long __cdecl strtoul(const char *, char **, int);
_CRTIMP double __cdecl wcstod(const wchar_t *, wchar_t **);//长字符串类型转换为数学类型
_CRTIMP long __cdecl wcstol(const wchar_t *, wchar_t **, int);
_CRTIMP unsigned long __cdecl wcstoul(const wchar_t *, wchar_t **, int);
_CRTIMP int __cdecl _wtoi(const wchar_t *);
_CRTIMP long __cdecl _wtol(const wchar_t *);
_CRTIMP __int64 __cdecl _wtoi64(const wchar_t *);
还有很多,请自行研究
1.2.CString及string,char *与其他数据类型的转换和操作
数学类型与CString相互转化
数学类型转化为CString
可用Format函数,举例:
CString s;
int i = 64;
s.Format("%d", i)
CString转换为数学类型:举例 CString strValue("1.234");
double dblValue;
dblValue = atof((LPCTSTR)strValue);
●CString与char*相互转换举例
CString strValue(“Hello”);
char *szValue;
szValue=strValue.GetBuffer(szValue);
也可用(LPSTR)(LPCTSTR)对CString// 进行强制转换.
szValue=(LPSTR)(LPCTSTR)strValue;
反过来可直接赋值:
char *szChar=NULL;
CString strValue;
szChar=new char[10];
memset(szChar,0,10);
strcpy(szChar,”Hello”);
strValue=szChar;
●CString 与 BSTR 型转换
CString 型转化成 BSTR 型
当我们使用 ActiveX 控件编程时,经常需要用到将某个值表示成 BSTR 类型.BSTR 是一种记数字符串,Intel平台上的宽字符串(Unicode),并且可以包含嵌入的 NULL 字符。
可以调用 CString 对象的 AllocSysString 方法将 CString 转化成 BSTR:
CString str;
str = .....; // whatever
BSTR bStr = str.AllocSysString();
BSTR型转换为CString
如果你在 UNICODE 模式下编译代码,你可以简单地写成:
CString convert(BSTR bStr)
{
if(bStr == NULL)
return CString(_T(""));
CString s(bStr); // in UNICODE mode
return s;
}
如果是 ANSI 模式
CString convert(BSTR b)
{
CString s;
if(b == NULL)
return s; // empty for NULL BSTR
#ifdef UNICODE
s = b;
#else
LPSTR p = s.GetBuffer(SysStringLen(b) + 1);
::WideCharToMultiByte(CP_ACP, // ANSI Code Page
0, // no flags
b, // source widechar string
-1, // assume NUL-terminated
p, // target buffer
SysStringLen(b)+1, // target buffer length
NULL, // use system default char
NULL); // don''t care if default used
s.ReleaseBuffer();
#endif
return s;
}
VC++常用数据类型及其操作详解
一.VC常用数据类型列表
|
|
Type |
Default Size |
Description |
|
基
础
类
型
全
是
小
写 |
说明:这些基础数据类型对于MFC还是API都是被支持的 |
|
boolean |
unsigned 8 bit , |
取值TRUE/FALSE |
|
byte |
unsigned 8 bit, |
整数,输出按字符输出 |
|
char |
unsigned 8 bit, |
字符 |
|
double |
signed 64 bit |
浮点型 |
|
float |
signed32 bit |
浮点型 |
|
handle_t |
|
Primitive handle type |
|
hyper |
signed 64 bit |
整型 |
|
int |
signed 32 bit |
整型 |
|
long |
signed 32 bit |
整型 |
|
short |
signed 16 bit |
整型 |
|
small |
signed 8 bit |
整型 |
|
void * |
32-bit |
指向未知类型的指针 |
|
wchar_t |
unsigned 16 bit |
16位字符,比char可容纳更多的字符 |
|
|
|
|
|
Win32
API
常
用
数
据
类
型
全
大
写 |
说明: 这些Win32API支持的简单数据类型主要是用来定义函数返回值,消息参数,结构成员。这类数据类型大致可以分为五大类:字符型、布尔型、整型、指针型和句柄型(?). 总共大概有100多种不同的类型, |
|
BOOL/BOOLEAN |
8bit,TRUE/FALSE |
布尔型 |
|
BYTE |
unsigned 8 bit |
|
|
BSTR
CComBSTR
_bstr_t |
32 bit |
BSTR是指向字符串的32位指针
是对BSTR的封装
是对BSTR的封装 |
|
CHAR |
8 bit |
(ANSI)字符类型 |
|
COLORREF |
32 bit |
RGB颜色值 整型 |
|
DWORD |
unsigned 32 bit |
| |