您的位置:首页 > 其它

CString,int,string,char,BSTR之间的转换

2013-06-28 22:47 471 查看
CString,int,string,char*之间的转换

string 转 CString

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

char 转 CString

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

char 转 string

string s(char *);

string 转 char *

char *p = string.c_str();

CString 转 string

string s(CString.GetBuffer());

1,string -> CString

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

用c_str()确实比data()要好.

2,char -> string

string s(char *);

你的只能初始化,在不是初始化的地方最好还是用assign().

3,CString -> string

string s(CString.GetBuffer());

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

《C++标准函数库》中说的

有三个函数可以将字符串的内容转换为字符数组和C—string

1.data(),返回没有”\0“的字符串数组

2,c_str(),返回有”\0“的字符串数组

3,copy()

CString互转int

将字符转换为整数,可以使用atoi、_atoi64或atol。

而将数字转换为CString变量,可以使用CString的Format函数。如

CString s;

int i = 64;

s.Format("%d", i)

Format函数的功能很强,值得你研究一下。

void CStrDlg::OnButton1()

{

// TODO: Add your control notification handler code here

CString

ss="1212.12";

int temp=atoi(ss);

CString aa;

aa.Format("%d",temp);

AfxMessageBox("var is " + aa);

}

sart.Format("%s",buf);

CString互转char*

///char * TO cstring

CString strtest;

char * charpoint;

charpoint="give string a value";

strtest=charpoint;

///cstring TO char *

charpoint=strtest.GetBuffer(strtest.GetLength());

标准C里没有string,char *==char []==string

可以用CString.Format("%s",char *)这个方法来将char *转成CString。要把CString转成char *,用操作符(LPCSTR)CString就可以了。

CString转换 char[100]

char a[100];

CString str("aaaaaa");

strncpy(a,(LPCTSTR)str,sizeof(a));

CString类型的转换成int

CString类型的转换成int

将字符转换为整数,可以使用atoi、_atoi64或atol。

//CString aaa = "16" ;

//int int_chage = atoi((lpcstr)aaa) ;

而将数字转换为CString变量,可以使用CString的Format函数。如

CString s;

int i = 64;

s.Format("%d", i)

Format函数的功能很强,值得你研究一下。

如果是使用char数组,也可以使用sprintf函数。

//CString ss="1212.12";

//int temp=atoi(ss);

//CString aa;

//aa.Format("%d",temp);

数字->字符串除了用CString::Format,还有FormatV、sprintf和不需要借助于Afx的itoa

3 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);

CString,int,string,char*之间的转换

string aa("aaa");

char *c=aa.c_str();

cannot convert from 'const char *' to 'char *'

const char *c=aa.c_str();

CString,int,string,char*之间的转换

string.c_str()只能转换成const char *,

要转成char *这样写:

string mngName;

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

BSTR转换成char*

方法一,使用ConvertBSTRToString。例如:

#include #pragma comment(lib, "comsupp.lib")

int _tmain(int argc, _TCHAR* argv[])

{

BSTR bstrText = ::SysAllocString(L"Test");

char* lpszText2 = _com_util::ConvertBSTRToString(bstrText);

SysFreeString(bstrText); // 用完释放

delete[] lpszText2;

return 0;

} 方法二,使用_bstr_t的赋值运算符重载。例如:

_bstr_t b = bstrText;

char* lpszText2 = b;

char*转换成BSTR

方法一,使用SysAllocString等API函数。例如:

BSTR bstrText = ::SysAllocString(L"Test");

BSTR bstrText = ::SysAllocStringLen(L"Test",4);

BSTR bstrText = ::SysAllocStringByteLen("Test",4); 方法二,使用COleVariant或_variant_t。例如:

//COleVariant strVar("This is a test");

_variant_t strVar("This is a test");

BSTR bstrText = strVar.bstrVal; 方法三,使用_bstr_t,这是一种最简单的方法。例如:

BSTR bstrText = _bstr_t("This is a test"); 方法四,使用CComBSTR。例如:

BSTR bstrText = CComBSTR("This is a test"); 或

CComBSTR bstr("This is a test");

BSTR bstrText = bstr.m_str; 方法五,使用ConvertStringToBSTR。例如:

char* lpszText = "Test";

BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);

CString转换成BSTR

通常是通过使用CStringT::AllocSysString来实现。例如:

CString str("This is a test");

BSTR bstrText = str.AllocSysString();



SysFreeString(bstrText); // 用完释放

BSTR转换成CString

一般可按下列方法进行:

BSTR bstrText = ::SysAllocString(L"Test");

CStringA str;

str.Empty();

str = bstrText; 或

CStringA str(bstrText);

ANSI、Unicode和宽字符之间的转换

方法一,使用MultiByteToWideChar将ANSI字符转换成Unicode字符,使用WideCharToMultiByte将Unicode字符转换成ANSI字符。

方法二,使用“_T”将ANSI转换成“一般”类型字符串,使用“L”将ANSI转换成Unicode,而在托管C++环境中还可使用S将ANSI字符串转换成String*对象。例如:

TCHAR tstr[] = _T("this is a test");

wchar_t wszStr[] = L"This is a test";

String* str = S”This is a test”; 方法三,使用ATL 7.0的转换宏和类。ATL7.0在原有3.0基础上完善和增加了许多字符串转换宏以及提供相应的类,它具有如图3所示的统一形式:

其中,第一个C表示“类”,以便于ATL 3.0宏相区别,第二个C表示常量,2表示“to”,EX表示要开辟一定大小的缓冲。SourceType和DestinationType可以是A、 T、W和OLE,其含义分别是ANSI、Unicode、“一般”类型和OLE字符串。例如,CA2CT就是将ANSI转换成一般类型的字符串常量。下面是一些示例代码:

LPTSTR tstr= CA2TEX<16>("this is a test");

LPCTSTR tcstr= CA2CT("this is a test");

wchar_t wszStr[] = L"This is a test";

char* chstr = CW2A(wszStr);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: