• <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>

            CString, QString, char*之間的轉換

            傳給未分配內存的const char* (LPCTSTR)指針.
               CString cstr(asdd);
               const char* ch = (LPCTSTR)cstr;
               ch指向的地址和cstr相同。但由于使用const保證ch不會修改,所以安全.2.傳給未分配內存的指針.
                CString cstr = "ASDDSD";
                char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
                cstr.ReleaseBuffer();
                //修改ch指向的值等于修改cstr里面的值.
                //PS:用完ch后,不用delete ch,因為這樣會破壞cstr內部空間,容易造成程序崩潰.
            3.第二種用法。把CString 值賦給已分配內存的char *。
                CString cstr1 = "ASDDSD";
                int strLength = cstr1.GetLength() + 1;
                char *pValue = new char[strLength];
                strncpy(pValue, cstr1, strLength);
            4.第三種用法.把CString 值賦給已分配內存char[]數組.
                CString cstr2 = "ASDDSD";
                int strLength1 = cstr1.GetLength() + 1;
                char chArray[100];
                memset(chArray,0, sizeof(bool) * 100); //將數組的垃圾內容清空.
                strncpy(chArray, cstr1, strLength1);

            如果上述都不行:
            CString轉換為char*
                    CString origCString("Hello, World!");
                    wchar_t* wCharString = origCString.GetBuffer(origCString.GetLength()+1);
                    size_t origsize = wcslen(wCharString) + 1;
                    size_t convertedChars = 0;
                    char *CharString;
                    CharString=new char(origsize);
                    wcstombs_s(&convertedChars, CharString, origsize, wCharString , _TRUNCATE);
                    cout << CharString << endl;
            成功輸出字符串"Hello,World"

            原因:
            原來在VC++ 2005以前,應用程序默認都是關閉對Unicode的支持的,而在VC2005中,默認打開了對它的支持,CString對應的字符串應該是TCHAR,TCHAR的定義是這樣的,
                    #ifdef _UNICODE
                    typedef wchar_t TCHAR    ;
                    #else
                    typedef char TCHAR;
                    #endif
            所以在工程中應該可以關閉對于Unicode的支持,從而可以直接轉換。這個做法是右擊工程名—〉Property—〉General中的character set中選擇notset,這樣,本文開頭的那段代碼就可以正確的執行了。


            如何將QString轉換為char *或者相反

            How can I convert a QString to char* and vice versa ?(trolltech)

            Answer:
            In order to convert a QString to a char*, then you first need to get a latin1 representation of the string by calling toLatin1() on it which will return a QByteArray. Then call data() on the QByteArray to get a pointer to the data stored in the byte array. See the documentation:

            See the following example for a demonstration:

            int main(int argc, char **argv)
            {
             QApplication app(argc, argv);
             QString str1 = "Test";
             QByteArray ba = str1.toLatin1();
             const char *c_str2 = ba.data();
             printf("str2: %s", c_str2);
             return app.exec();  
            }
            Note that it is necessary to store the bytearray before you call data() on it, a call like the following
            const char *c_str2 = str2.toLatin1().data();

            will make the application crash as the QByteArray has not been stored and hence no longer exists.


            To convert a char* to a QString you can use the QString constructor that takes a QLatin1String, e.g:

            QString string = QString(QLatin1String(c_str2)) ;

             

            還有其他多種方法:

            方法一 -----------------------------------------
            #define G2U(s) ( QTextCodec::codecForName("GBK")->toUnicode(s) )
            #define U2G(s) ( QTextCodec::codecForName("GBK")->fromUnicode(s) )

            QString str;
            QCString cstr;

            str = G2U("中文輸入");
            cstr = U2G(str);

            QCString有這樣一個重載運算符
            operator const char * () const

            可以這樣
            printf("%s\n", (const char*) cstr);
            或是copy出來
            char buf[1024];
            strcpy(buf, (const char*) cstr);

             

            方法二 -----------------------------------------
            如果是中文系統

            直接用   (const char*) str.local8Bit()
            例如
            printf("%s", (const char*) str.local8Bit());

            str是一個QString

            方法三 -----------------------------------------
            char str[64];
            QTextCodec *textcod = QTextCodec::codecForName("GBK");
                    QCString string1 = textcod ->fromUnicode(listbox1->currentText());
                    strcpy(str,string1);

            QString和Std::string

             從char*到 QString可以從fromLocal8Bit()轉化
            std::string有c_str()的函數使再轉化為char*

            QString有toAscii()記不清了


            你可以看看.


            又是我的粗心釀成大錯,我重新查看了一下Qt文檔,原來Qt可以直接從std::wstring產生一個QString,用QString::fromStdWString(const std::wstring &)這個靜態成員函數即可。我試了試用std::string的c_str()返回的char *構造的QString不能再保存原先的中文信息,而用std::wstring構造的QString則可以用qDebug()輸出原先的中文信息
            GB編碼與UTF8編碼的轉換
            在主函數app后加上這句:

            QUOTE:

            QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));

             

            然后是從UTF8編碼到GB編碼的字符串轉換方法:

            QUOTE:


            QString Utf8_To_GB(QString strText)
            {
                return QString::fromUtf8(strText.toLocal8Bit().data());
            }

             

            至于從GB到UTF8,那大家就經常用了:

            QUOTE:

            QString GB_To_Utf8(char *strText)
            {
                return QString::fromLocal8Bit(strText);
            }

            posted on 2008-11-19 21:39 Alina-zl 閱讀(13272) 評論(1)  編輯 收藏 引用

            評論

            # re: CString, QString, char*之間的轉換 2008-11-21 20:28 七星重劍

            好詳細啊  回復  更多評論   

            <2008年11月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            人妻无码中文久久久久专区| 久久夜色精品国产| 人妻精品久久无码专区精东影业| 亚洲AV无码久久寂寞少妇| 97精品伊人久久久大香线蕉| 久久久久亚洲AV成人网人人软件| 亚洲国产精品嫩草影院久久| 久久久久99精品成人片牛牛影视 | 久久A级毛片免费观看| 久久综合88熟人妻| 久久人人超碰精品CAOPOREN | 伊人丁香狠狠色综合久久| 99久久精品国内| 久久精品免费大片国产大片| 一级a性色生活片久久无少妇一级婬片免费放 | 久久久青草青青国产亚洲免观| 久久综合亚洲色HEZYO国产| 囯产极品美女高潮无套久久久| 香蕉久久一区二区不卡无毒影院 | 国产成人久久精品一区二区三区| 26uuu久久五月天| 亚洲精品国产字幕久久不卡| 青青青国产成人久久111网站| 人妻精品久久久久中文字幕69 | 久久国产成人精品麻豆| 国产精品久久久久一区二区三区| 亚洲熟妇无码另类久久久| 国产一区二区三精品久久久无广告| 久久99久国产麻精品66| 久久亚洲天堂| 国产精品VIDEOSSEX久久发布| 91久久精一区二区三区大全| 欧美午夜A∨大片久久| 久久99国产一区二区三区| 伊人久久综在合线亚洲2019| 99久久免费国产特黄| 精品久久久久久久无码| AV色综合久久天堂AV色综合在 | 久久亚洲精精品中文字幕| 99久久精品国产一区二区| 亚洲中文字幕伊人久久无码|