• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            windows核心編程--字符集

             字符unicode與windows

             
            1 軟件的本地化要解決的真正問題,實(shí)際上就是如何來處理不同的字符集。以前我們習(xí)慣與用單字節(jié)字符集來編程.
            2 單字符集:將文本串作為一系列單字節(jié)字符來進(jìn)行編碼,并在結(jié)尾處放上一個(gè)零。(每個(gè)字符用一個(gè)字節(jié)來表示)
            3 雙字節(jié)字符集(D B C S ):在雙字節(jié)字符集中,字符串中的每個(gè)字符可以包含一個(gè)字節(jié)或包含兩個(gè)字節(jié)。
            4 unicode字符集:U n i c o d e 提供了一種簡(jiǎn)單而又一致的表示字符串的方法。U n i c o d e 字符串中的所有字符都是1 6 位的(兩個(gè)字節(jié))。
            5 當(dāng)M i c r o s o f t 公司將C O M 從1 6 位Wi n d o w s 轉(zhuǎn)換成Wi n 3 2 時(shí),公司作出了一個(gè)決定,即需要字符串的所有C O M 接口方法都只能接受U n i c o d e 字符串。
            6 c運(yùn)行期庫支持unicode,即使是windows98也支持.
            7 Windows 2000 的N o t e p a d (記事本)應(yīng)用程序允許你既能打開U n i c o d e 文件,也能打開A N S I 文件,并且可以創(chuàng)建這些文件。
            8 I s Te x t U n i c o d e 函數(shù)能夠幫助進(jìn)行區(qū)分ANSIC字符和unicode:
            DWORD IsTextUnicode(CONST PVOID pvBuffer, int cb,PINT pResult);

            第一個(gè)參數(shù)p v B u ff e r 用于標(biāo)識(shí)要測(cè)試的緩存的地址。該數(shù)據(jù)是個(gè)無效指針,因?yàn)槟悴恢滥銚碛械氖茿 N S I 字符數(shù)組還是U n i c o d e 
            字符數(shù)組。

            第二個(gè)參數(shù)c b 用于設(shè)定p v B u ff e r 指向的字節(jié)數(shù)。同樣,由于你不知道緩存中放的是什么,因此c b 是個(gè)字節(jié)數(shù),而不是字符數(shù)。請(qǐng)注意,不必設(shè)定緩存的整個(gè)長度。當(dāng)然,I s Te x t U n i c o d e能夠測(cè)試的字節(jié)越多,得到的結(jié)果越準(zhǔn)確。

            第三個(gè)參數(shù)p R e s u l t 是個(gè)整數(shù)的地址,必須在調(diào)用I s Te x t U n i c o d e 之前對(duì)它進(jìn)行初始化。對(duì)該整數(shù)進(jìn)行初始化后,就可以指明你要I s Te x t U n i c o d e 執(zhí)行哪些測(cè)試。也可以為該參數(shù)傳遞N U L L ,在這種情況下,I s Te x t U n i c o d e 將執(zhí)行它能夠進(jìn)行的所有測(cè)試(詳細(xì)說明請(qǐng)參見Platform SDK 文檔)。

            9 對(duì)D B C S 字符串進(jìn)行操作的幫助函數(shù)

            函數(shù) 描述
            PTSTR CharNext(PCTSTR pszCurrentChar); 返回字符串中的下一個(gè)字符的地址
            PTSTR CharPrev (PCTSTR pszStart,PCTSTR p s z C u r r e n t C h a r); 返回字符串中的上一個(gè)字符的地址
            BOOL IsDBCSLeadByteTRUE(BYTE bTestChar); 如果該字節(jié)是DBCS 字符的第一個(gè)字節(jié),則返回


            10 “M i c r o s o f t 公司對(duì)U n i c o d e 支持的情況”:

            • Windows 2000 既支持U n i c o d e ,也支持A N S I ,因此可以為任意一種開發(fā)應(yīng)用程序。

            • Windows 98 只支持A N S I ,只能為A N S I 開發(fā)應(yīng)用程序。

            • Windows CE 只支持U n i c o d e ,只能為U n i c o d e 開發(fā)應(yīng)用程序。

            11 Wi n d o w s 頭文件定義de Uincode 數(shù)據(jù)類型

            數(shù)據(jù)類型 說明
            W C H A R U n i c o d e 字符
            P W S T R 指向U n i c o d e 字符串的指針
            P C W S T R 指向一個(gè)恒定的U n i c o d e 字符串的指針
            使用實(shí)例如下:
            #ifdef UNICODE
            #define CreateWindowEx CreateWindowExW
            #else
            #define CreateWindowEx CreateWindowExA
            #endif //!UNICODE
            在Unicode與ANSI之間轉(zhuǎn)換字符串

            Wi n d o w s 函數(shù)M u l t i B y t e To Wi d e C h a r 用于將多字節(jié)字符串轉(zhuǎn)換成寬字符串。下面顯示了M u l t i B y t e To Wi d e C h a r 函數(shù)。

            int MultiByteToWideChar(
            UINT CodePage,          //code page
            DWORD dwFlags,          //character-type options
            LPCSTR lpMultiByteStr,  //address of string to map
            int cchMultiByte,       //number of bytes in string
            LPWSTR lpWideCharStr,   //address of wide-character buffer
            int cchWideChar         //size of buffer
            );
            u C o d e P a g e 參數(shù)用于標(biāo)識(shí)一個(gè)與多字節(jié)字符串相關(guān)的代碼頁號(hào)。d w F l a g s 參數(shù)用于設(shè)定另一個(gè)控件,它可以用重音符號(hào)之類的區(qū)分標(biāo)記來影響字符。這些標(biāo)志通常并不使用,在d w F l a g s參數(shù)中傳遞0 。p M u l t i B y t e S t r 參數(shù)用于設(shè)定要轉(zhuǎn)換的字符串,c c h M u l t i B y t e 參數(shù)用于指明該字符串的長度(按字節(jié)計(jì)算)。如果為c c h M u l t i B y t e 參數(shù)傳遞- 1 ,那么該函數(shù)用于確定源字符串的長度。

            轉(zhuǎn)換后產(chǎn)生的U n i c o d e 版本字符串將被寫入內(nèi)存中的緩存,其地址由p Wi d e C h a r S t r 參數(shù)指定。必須在c c h Wi d e C h a r 參數(shù)中設(shè)定該緩存的最大值(以字符為計(jì)量單位)。如果調(diào)用M u l t i B y t e To Wi d e C h a r ,給c c h Wi d e C h a r 參數(shù)傳遞0 ,那么該參數(shù)將不執(zhí)行字符串的轉(zhuǎn)換,而是返回為使轉(zhuǎn)換取得成功所需要的緩存的值。一般來說,可以通過下列步驟將多字節(jié)字符串轉(zhuǎn)換成U n i c o d e 等價(jià)字符串:

            1) 調(diào)用M u l t i B y t e To Wi d e C h a r 函數(shù),為p Wi d e C h a r S t r 參數(shù)傳遞N U L L ,為c c h Wi d e C h a r 參數(shù)傳遞0 。
            2) 分配足夠的內(nèi)存塊,用于存放轉(zhuǎn)換后的U n i c o d e 字符串。該內(nèi)存塊的大小由前面對(duì)M u l t B y t e To Wi d e C h a r 的調(diào)用返回。
            3) 再次調(diào)用M u l t i B y t e To Wi d e C h a r ,這次將緩存的地址作為p Wi d e C h a r S t r 參數(shù)來傳遞,并傳遞第一次調(diào)用M u l t i B y t e To Wi d e C h a r 時(shí)返回的緩存大小,作為c c h Wi d e c h a r 參數(shù)。
            4. 使用轉(zhuǎn)換后的字符串。
            5) 釋放U n i c o d e 字符串占用的內(nèi)存塊。
            函數(shù)Wi d e C h a r To M u l t i B y t e 將寬字符串轉(zhuǎn)換成等價(jià)的多字節(jié)字符串,如下所示:

            int WideCharToMultiByte(
            UINT CodePage,         // code page
            DWORD dwFlags,         // performance and mapping flags
            LPCWSTR lpWideCharStr, // address of wide-character string
            int cchWideChar,       // number of characters in string
            LPSTR lpMultiByteStr,  // address of buffer for new string
            int cchMultiByte,      // size of buffer
            LPCSTR lpDefaultChar,  // address of default for unmappable
            // characters
            LPBOOL lpUsedDefaultChar   // address of flag set when default
            // char. used
            );
            該函數(shù)與M u l t i B i t e To Wi d e C h a r 函數(shù)相似。同樣,u C o d e P a g e 參數(shù)用于標(biāo)識(shí)與新轉(zhuǎn)換的字符串相關(guān)的代碼頁。d w F l a g s 則設(shè)定用于轉(zhuǎn)換的其他控件。這些標(biāo)志能夠作用于帶有區(qū)分符號(hào)的字符和系統(tǒng)不能轉(zhuǎn)換的字符。通常不需要為字符串的轉(zhuǎn)換而擁有這種程度的控制手段,你將為d w F l a g s 參數(shù)傳遞0 。

            p Wi d e C h a r S t r 參數(shù)用于設(shè)定要轉(zhuǎn)換的字符串的內(nèi)存地址,c c h Wi d e C h a r 參數(shù)用于指明該字符串的長度(用字符數(shù)來計(jì)量)。如果你為c c h Wi d e C h a r 參數(shù)傳遞- 1 ,那么該函數(shù)用于確定源字符串的長度。

            轉(zhuǎn)換產(chǎn)生的多字節(jié)版本的字符串被寫入由p M u l t i B y t e S t r 參數(shù)指明的緩存。必須在c c h M u l t i B y t e參數(shù)中設(shè)定該緩存的最大值(用字節(jié)來計(jì)量)。如果傳遞0 作為Wi d e C h a r To M u l t i B y t e 函數(shù)的c c h M u l t i B y t e 參數(shù),那么該函數(shù)將返回目標(biāo)緩存需要的大小值。通常可以使用將多字節(jié)字符串轉(zhuǎn)換成寬字節(jié)字符串時(shí)介紹的一系列類似的事件,將寬字節(jié)字符串轉(zhuǎn)換成多字節(jié)字符串。

            你會(huì)發(fā)現(xiàn),Wi d e C h a r To M u l t i B y t e 函數(shù)接受的參數(shù)比M u l t i B y t e To Wi d e C h a r 函數(shù)要多2 個(gè),即p D e f a u l t C h a r 和p f U s e d D e f a u l t C h a r 。只有當(dāng)Wi d e C h a r To M u l t i B y t e 函數(shù)遇到一個(gè)寬字節(jié)字符,而該字符在u C o d e P a g e 參數(shù)標(biāo)識(shí)的代碼頁中并沒有它的表示法時(shí),Wi d e C h a r To M u l t i B y t e 函數(shù)才使用這兩個(gè)參數(shù)。如果寬字節(jié)字符不能被轉(zhuǎn)換,該函數(shù)便使用p D e f a u l t C h a r 參數(shù)指向的字符。如果該參數(shù)是N U L L (這是大多數(shù)情況下的參數(shù)值),那么該函數(shù)使用系統(tǒng)的默認(rèn)字符。該默認(rèn)字符通常是個(gè)問號(hào)。這對(duì)于文件名來說是危險(xiǎn)的,因?yàn)閱柼?hào)是個(gè)通配符。

            p f U s e d D e f a u l t C h a r 參數(shù)指向一個(gè)布爾變量,如果寬字符串中至少有一個(gè)字符不能轉(zhuǎn)換成等價(jià)多字節(jié)字符,那么函數(shù)就將該變量置為T R U E 。如果所有字符均被成功地轉(zhuǎn)換,那么該函數(shù)就將該變量置為FA L S E 。當(dāng)函數(shù)返回以便檢查寬字節(jié)字符串是否被成功地轉(zhuǎn)換后,可以測(cè)試該變量。同樣,通常為該測(cè)試傳遞N U L L 。

            關(guān)于如何使用這些函數(shù)的詳細(xì)說明,請(qǐng)參見Platform SDK 文檔。

            如果使用這兩個(gè)函數(shù),就可以很容易創(chuàng)建這些函數(shù)的U n i c o d e 版本和A N S I 版本。例如,你可能有一個(gè)動(dòng)態(tài)鏈接庫,它包含一個(gè)函數(shù),能夠轉(zhuǎn)換字符串中的所有字符??梢韵裣旅孢@樣編寫該函數(shù)的U n i c o d e 版本:

            BOOL StringReverseW(PWSTR pWideCharStr)
            {
            //Get a pointer to the last character in the string.
            PWSTR pEndOfStr=pWideCharStr+wcslen(pWideCharStr)-1;
            wchar_t cCharT;
            //Repeat until we reach the center character
            //in the string.
            while (pWideCharStr < pEndOfStr)
            {
            //Save a character in a temporary variable.
            cCharT=*pWideCharStr;
            //Put the last character in the first character.
            *pWideCharStr =*pEndOfStr;
            //Put the temporary character in the last character.
            *pEndOfStr=cCharT;
            //Move in one character from the left.
            pWideCharStr++;
            //Move in one character from the right.
            pEndOfStr--;
            }
            //The string is reversed; return success.
            return(TRUE);
            }

            你可以編寫該函數(shù)的A N S I 版本以便該函數(shù)根本不執(zhí)行轉(zhuǎn)換字符串的實(shí)際操作。你也可以編寫該函數(shù)的A N S I 版本,以便該函數(shù)它將A N S I 字符串轉(zhuǎn)換成U n i c o d e 字符串,將U n i c o d e 字符串傳遞給S t r i n g R e v e r s e W 函數(shù),然后將轉(zhuǎn)換后的字符串重新轉(zhuǎn)換成A N S I 字符串。該函數(shù)類似下面的樣子:

             

            BOOL StringReverseA(PSTR pMultiByteStr)
            {
            PWSTR pWideCharStr;
            int nLenOfWideCharStr;
            BOOL fOk = FALSE;
            //Calculate the number of characters needed to hold
            //the wide_character version of string.
            nLenOfWideCharStr = MultiRyteToWideChar(CP_ACP, 0,
            pMultiByteStr, -1, NULL, 0);
            //Allocate memory from the process's default heap to
            //accommodate the size of the wide-character string.
            //Don't forget that MultiByteToWideChar returns the
            //number of characters,not the number of bytes,so
            //you must multiply by the size of wide character.
            pWideCharStr = HeapAlloc(GetProcessHeap(), 0,
            nLenOfWideCharStr * sizeof(WCHAR));
            if (pWideCharStr == NULL)
            return(fOk);
            //Convert the multibyte string to a wide_character string.
            MultiByteToWideChar(CP_ACP, 0, pMulti8yteStr, -1,
            pWideCharStr, nLenOfWideCharStr);
            //Call the wide-character version of this
            //function to do the actual work
            fOk = StnngReverseW(pWideCharStr);
            if (fOk)
            {
            //Convert the wide-character string back
            //to a multibyte string.
            WideCharToMultiByte(CP_ACP, 0, pWideCharStr, -1,
            pMultiByteStr, strlen(pMultiByteStr), NULL, NULL);
            }
            //Free the momory containing the wide-character string.
            HeapFree(GetProcessHeap(), 0, pWideCharStr);
            return(fOk),
            }
            最后,在用動(dòng)態(tài)鏈接庫分配的頭文件中,可以像下面這樣建立這兩個(gè)函數(shù)的原型:

             

            BOOL StringReverseW (PWSTR pWideCharStr);
            BOOL StringReverseA (PSTR pMultiByteStr);
            #ifdef UNICODE
            #define StnngReverse StringReverseW
            #else
            #define StringRevcrsc StringReverseA
            #endif // UNICODE

            posted on 2006-09-11 16:57 夢(mèng)在天涯 閱讀(1862) 評(píng)論(2)  編輯 收藏 引用 所屬分類: Windows API

            評(píng)論

            # re: windows核心編程--字符集 2006-09-12 08:50 笨笨

            大家都在搞核心編程??!這本書值得好好研究  回復(fù)  更多評(píng)論   

            # re: windows核心編程--字符集 2006-09-12 09:18 夢(mèng)在天涯

            恩,是的,值的好好研究啊,很難啊,但是可以學(xué)到windows的工作原理,有助于更好地在windows上編程!  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804173
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            性欧美丰满熟妇XXXX性久久久| 99re这里只有精品热久久| 精品久久久无码中文字幕| 精品久久久久久国产三级| 久久久久亚洲AV成人网人人网站 | 日本五月天婷久久网站| 久久久久久午夜成人影院| 2020最新久久久视精品爱| 久久人人青草97香蕉| 精品人妻久久久久久888| 久久精品亚洲福利| 久久国产精品一国产精品金尊| 精品国产青草久久久久福利| 久久久久久久久波多野高潮| 久久亚洲国产欧洲精品一| 久久精品国产色蜜蜜麻豆| 国产成人久久777777| 中文精品久久久久人妻不卡| 93精91精品国产综合久久香蕉| 亚洲中文字幕无码久久2020| 久久久WWW成人免费精品| 久久99国产精品一区二区| 国色天香久久久久久久小说 | 精品久久久久久国产| 情人伊人久久综合亚洲| 色偷偷88888欧美精品久久久| 国产视频久久| 国产综合精品久久亚洲| 色噜噜狠狠先锋影音久久| 久久精品国产第一区二区三区| 久久只这里是精品66| 美女久久久久久| 久久久久国色AV免费看图片| 国产精品伦理久久久久久| 国产国产成人久久精品| 91久久精品国产免费直播| 久久精品国产久精国产| 99久久99久久精品国产| 久久成人影院精品777| 亚洲国产天堂久久综合网站| 精品一区二区久久久久久久网站|