• <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++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(8)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜


            注意,所有的代碼,建立的是win32 console application 支持MFC,因?yàn)镃String,只有MFC才支持
            4、 char*和CString的相互轉(zhuǎn)換

                  CString包含了3個(gè)值:指向某個(gè)數(shù)據(jù)緩沖區(qū)的指針、該緩沖區(qū)中有效地字符記數(shù)(它是不可存取的,是位于CString地址之下的一個(gè)隱藏區(qū)域)及一個(gè)緩沖區(qū)長(zhǎng)度。有效字符數(shù)的大小可以使從0到該緩沖最大長(zhǎng)度值減1之間的任何數(shù)(因?yàn)樽址Y(jié)尾有一個(gè)NULL字符)

                  4.1 char*轉(zhuǎn)換為CString

                   ①直接賦值

                     ②利用格式化轉(zhuǎn)換

            #include "stdafx.h"
            #include 
            "CString.h"
            #ifdef _DEBUG
            #define new DEBUG_NEW
            #undef THIS_FILE
            static char THIS_FILE[] = __FILE__;
            #endif
            /////////////////////////////////////////////////////////////////////////////
            // The one and only application object
            CWinApp theApp;
            using namespace std;
            int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
            {
                
            int nRetCode = 0;
                
            // initialize MFC and print and error on failure
                if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
                
            {
                    
            // TODO: change error code to suit your needs
                    cerr << _T("Fatal Error: MFC initialization failed"<< endl;
                    nRetCode 
            = 1;
                }

                
            else
                
            {
                    
            // TODO: code your application's behavior here.
                    CString strHello;
                    strHello.LoadString(IDS_HELLO);
                    cout 
            << (LPCTSTR)strHello << endl;
                    CString strConvert;
                    TCHAR
            * p= _T("this is a chToString test ");
                    
            //要試驗(yàn)的話,只需要去掉注釋和加上注釋
                    strConvert = p;//直接復(fù)制
                    
            //strConvert.Format("%s",p);//格式化
                    
            //注意,這里不能直接cout<<strConvert,輸出的會(huì)是一串?dāng)?shù)字
                    cout<<"strConvert="<<(LPCTSTR)strConvert<<endl;
                }

                
            return nRetCode;
            }

                  4.2 CString轉(zhuǎn)換為char*

                  ①?gòu)?qiáng)制類型轉(zhuǎn)換為L(zhǎng)PCTSTR

                  ②使用strcpy

                  需要說(shuō)明的是,strcpy(或可移植的_tcscpy)的第二個(gè)參數(shù)是const wchar_t* (Unicode)或const char* (ANSI),系統(tǒng)編譯器將會(huì)自動(dòng)對(duì)其進(jìn)行轉(zhuǎn)換。

            #include "stdafx.h"
            #include 
            "CString.h"
            #ifdef _DEBUG
            #define new DEBUG_NEW
            #undef THIS_FILE
            static char THIS_FILE[] = __FILE__;
            #endif
            /////////////////////////////////////////////////////////////////////////////
            // The one and only application object
            CWinApp theApp;
            using namespace std;
            int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
            {
                
            int nRetCode = 0;
                
            // initialize MFC and print and error on failure
                if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
                
            {
                    
            // TODO: change error code to suit your needs
                    cerr << _T("Fatal Error: MFC initialization failed"<< endl;
                    nRetCode 
            = 1;
                }

                
            else
                
            {
                    
            // TODO: code your application's behavior here.
                    CString strHello;
                    strHello.LoadString(IDS_HELLO);
                    cout 
            << (LPCTSTR)strHello << endl;
                    
            //強(qiáng)制類型轉(zhuǎn)換為L(zhǎng)PCTSTR
                    CString theString( (_T("Char test ")));
                    LPTSTR lpsz
            =(LPTSTR)(LPCTSTR)theString;
                    
            //使用strcpy
                    LPTSTR lpsz1=new TCHAR[theString.GetLength()+1];
                    _tcscpy(lpsz,theString);
                }

                
            return nRetCode;
            }

                     4.3使用GetBuffer

                    如果需要修改CString中的內(nèi)容,它有一個(gè)特殊的方法可以使用,那就是GetBuffer,它的作用是返回一個(gè)可寫(xiě)的緩沖指針。如果只是打算修改字符或者截短字符串,例如

            CString theString( (_T("Char test ")));
            LPTSTR lpsz
            =s.GetBuffer();
            /*添加p的代碼*/
            s.ReleaseBuffer();
            //使用完后及時(shí)釋放


            如果還想獲得更多關(guān)于《Visual C++代碼參考與技巧大全》的內(nèi)容,可點(diǎn)擊下面網(wǎng)址,

            http://www.shnenglu.com/kangnixi/archive/2010/01/13/105591.html
            久久人人超碰精品CAOPOREN| 中文字幕久久精品| 精品久久久久香蕉网| 精品无码久久久久久尤物| 成人a毛片久久免费播放| 亚洲国产成人久久综合一区77| 一本色道久久88综合日韩精品 | 中文字幕久久欲求不满| 国产精品欧美久久久久无广告| 欧美成人免费观看久久| 91久久婷婷国产综合精品青草 | 韩国无遮挡三级久久| 久久夜色撩人精品国产| 久久婷婷五月综合97色一本一本| 国产成人精品久久亚洲高清不卡 | 波多野结衣久久一区二区| 青青草原1769久久免费播放 | 国产精品无码久久综合 | 亚洲AⅤ优女AV综合久久久| 久久ZYZ资源站无码中文动漫| 性做久久久久久久久| 狠狠人妻久久久久久综合| 久久精品www人人爽人人| 久久成人小视频| 久久夜色精品国产www| 中文字幕一区二区三区久久网站| 久久国产欧美日韩精品| 久久综合九色综合欧美就去吻| 久久久久免费精品国产| 99久久精品国内| 国产亚洲色婷婷久久99精品| 99久久精品免费看国产一区二区三区| 久久精品二区| 国产精品免费久久久久电影网| 伊人丁香狠狠色综合久久| 99久久人人爽亚洲精品美女| 久久电影网2021| 99热热久久这里只有精品68| 国产精品欧美久久久久无广告 | 国产精品亚洲综合久久| 亚洲国产精品成人AV无码久久综合影院|