VC 中資料型別的轉換

2021-08-25 20:16:16 字數 3430 閱讀 4498

引用自施昌權-部落格

1、char*轉換成cstring

若將char*轉換成cstring,除了直接賦值外,還可使用cstring::format進行。例如:

char charray = "this is a test";

char * p = "this is a test";

或lpstr p = "this is a test"; 或在已定義unicode應的用程式中

tchar * p = _t("this is a test"); 或

lptstr p = _t("this is a test");

cstring thestring = charray;

thestring.format(_t("%s"), charray);

thestring = p;

2、cstring轉換成char*

若將cstring類轉換成char*(lpstr)型別,常常使用下列三種方法:

方法一,使用強制轉換。例如:

cstring thestring( "this is a test" );

lptstr lpsz =(lptstr)(lpctstr)thestring; 方法二,使用strcpy。例如:

cstring thestring( "this is a test" );

lptstr lpsz = new tchar[thestring.getlength()+1];

_tcscpy(lpsz, thestring); 需要說明的是,strcpy(或可移值unicode/mbcs的_tcscpy)的第二個引數是 const wchar_t* (unicode)或const char* (ansi),系統編譯器將會自動對其進行轉換。

方法三,使用cstring::getbuffer。例如:

cstring s(_t("this is a test "));

lptstr p = s.getbuffer();

// 在這裡新增使用p的**

if(p != null) *p = _t('/0');

s.releasebuffer(); // 使用完後及時釋放,以便能使用其它的cstring成員函式

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

4、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);

5、cstring轉換成bstr

通常是通過使用cstringt::allocsysstring來實現。例如:

cstring str("this is a test");

bstr bstrtext = str.allocsysstring();

sysfreestring(bstrtext); // 用完釋放

6、bstr轉換成cstring

一般可按下列方法進行:

bstr bstrtext = ::sysallocstring(l"test");

cstringa str;

str.empty();

str = bstrtext; 或

cstringa str(bstrtext);

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

VC中資料型別轉換

cstring tchar 的轉化可以用函式getbuff 函式原型為 lptstr getbuffer int nminbuflength cstring str cstring tchar szmsg new tchar 100 其引數為cstring字串的長度 szmsg str.getbuf...

vc 資料型別轉換

剛接觸vc程式設計的朋友往往對許多資料型別的轉換感到迷惑不解,本文將介紹一些常用資料型別的使用。我們先定義一些常見型別變數藉以說明 int i 100 long l 2001 float f 300.2 double d 12345.119 char username 女俠程佩君 char temp...

vc 資料型別轉換

技術開發 2007 01 29 16 00 13 閱讀352 字型大小 大 中小訂閱 數學型別變數與字串相互轉換 這些函式都在stdlib.h裡 1 將數學型別轉換為字串可以用以下一些函式 舉例 crtimp char cdecl itoa int,char int 這是乙個將數字轉換為乙個字串型別...