LPTSTR 及字串相關

2021-05-25 00:02:36 字數 3033 閱讀 2135

lptstr解釋  與char*等價,表示普通字元/字串變數,指向字元/字串的指標。

lp:長指標(long pointer)。

t:win32環境中有乙個_t巨集,用來標識字元是否採用unicode編碼(兩位元組表示乙個字元),若程式中定義了unicode,該字元/字串被作為unicode字串,否則就是標準的ansi(單位元組表示乙個字元)字串。

str:表示這個變數是乙個字串。

/* lptstr 轉換成 cstring */

(1)直接賦值

cstring strtext;

lptstr lpsztext = _t("lptstr >> cstring");

strtext = lpsztext;

::messagebox( null, strtext , _t("標題"), mb_iconasterisk|mb_taskmodal|mb_ok );

(2)cstring::format()格式化

cstring strtext;

lptstr lpsztext = _t("lptstr >> cstring");

strtext.format( _t("%s"), lpsztext );

::messagebox( null, strtext , _t("標題"), mb_iconasterisk|mb_taskmodal|mb_ok );

/* cstring 轉換成 lptstr */

(1)強制轉換

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

lptstr lpsztext =(lptstr)(lpctstr)strtext;

::messagebox( null, lpsztext, _t("標題"), mb_iconasterisk|mb_taskmodal|mb_ok );

(2)使用lstrcpy()

cstring strtext( "this is a test" );

lptstr lpsztext = new tchar[strtext.getlength()+1];

lstrcpy( lpsztext, strtext );

::messagebox( null, lpsztext, _t("標題"), mb_iconasterisk|mb_taskmodal|mb_ok );

(3)使用cstring::getbuffer()

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

lptstr lpsztext = strtext.getbuffer();

strtext.releasebuffer();

::messagebox( null, lpsztext, _t("標題"), mb_iconasterisk|mb_taskmodal|mb_ok );

/* char * 轉換成 cstring

char charray = "this is a test";

char * p = "this is a test";

cstring thestring = charray;

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

thestring = p;

/* cstring轉換成char*

1. cstring thestring( "this is a test" );

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

_tcscpy(lpsz, thestring);

2. cstring s(_t("char test "));

lptstr p = s.getbuffer(); [1]

lptstr dot = strchr(p, ''.'');

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

if(p != null)

*p = _t('');

s.releasebuffer();

lpctstr,lpwstr, ptstr, lptstr,wchar_t區別

l表示long指標,這是為了相容windows 3.1等16位作業系統遺留下來的,在win32中以及其他的32為作業系統中, long指標和near指標及far修飾符都是為了相容的作用,沒有實際意義。即win32中,long,near,far指標與普通指標沒有區別,lp與p是等效的。

p表示這是乙個指標。

t表示_t巨集,這個巨集用來表示你的字元是否使用unicode, 如果你的程式定義了unicode或者其他相關的巨集,那麼這個字元或者字串將被作為unicode字串,否則就是標準的ansi字串。

str表示這個變數是乙個字串。

c表示是乙個常量,const。

lptstr:

如果定義了unicode巨集則lptstr被定義為lpwstr。typedef lptstr lpwstr;

否則lptstr被定義為lpstr。 typedef lptstr lpstr;

下面列出一些常用的typedefs:

型別 mbcs unicode

wchar wchar_t wchar_t

lpstr char* char*

lpcstr const char* const char*

lpwstr wchar_t* wchar_t*

lpcwstr const wchar_t* const wchar_t*

tchar char wchar_t

lptstr tchar*(或char*) tchar* (或wchar_t*)

lpctstr const tchar* const tchar*

由於win32 api文件的函式列表使用函式的常用名字(例如, setwindowtext"),所有的字串都是用tchar來定義的。(除了xp中引入的只適用於unicode的api)。所以結論,在vs2005系統中,為提高可移植性,定義字串時用tchar,轉化為unicode時用_t而不用l。

字串及相關習題

例題1 翻譯密碼 密碼是我們生活中非常重要的東東,我們的那麼一點不能說的秘密就全靠它了。哇哈哈.接下來淵子要在密碼之上再加一套密碼,雖然簡單但也安全。假設淵子原來乙個bbs上的密碼為zvbo9441987,為了方便記憶,他通過一種演算法把這個密碼變換成yuanzhi1987,這個密碼是他的名字和出生...

字串相關

30 字串相關 30.1追加字元 nsmutablestring string nsmutablestring alloc init nsstring stroneintro info stringbyreplacingoccurrencesofstring withstring 30.3字串比較 ...

字串相關

字串轉換相關部落格 使用stringstream字串轉數字 include include includeusing namespace std int main 使用sscanf 進行字串轉數字char str 1234321 int a sscanf str,d a char str 123.3...