• <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*之間的轉(zhuǎn)換

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

            如果上述都不行:
            CString轉(zhuǎn)換為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"

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


            如何將QString轉(zhuǎn)換為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有這樣一個(gè)重載運(yùn)算符
            operator const char * () const

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

             

            方法二 -----------------------------------------
            如果是中文系統(tǒng)

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

            str是一個(gè)QString

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

            QString和Std::string

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

            QString有toAscii()記不清了


            你可以看看.


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

            QUOTE:

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

             

            然后是從UTF8編碼到GB編碼的字符串轉(zhuǎn)換方法:

            QUOTE:


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

             

            至于從GB到UTF8,那大家就經(jīng)常用了:

            QUOTE:

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

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

            評(píng)論

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

            好詳細(xì)啊  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


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

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            国产精品伦理久久久久久| 国产91久久综合| 久久九九兔免费精品6| 久久丫忘忧草产品| 亚洲中文精品久久久久久不卡| 久久受www免费人成_看片中文| 久久婷婷国产剧情内射白浆| 久久精品中文闷骚内射| 亚洲一区二区三区日本久久九| 日本精品一区二区久久久| 亚洲精品无码久久千人斩| 欧美综合天天夜夜久久| 久久无码AV中文出轨人妻| 精品久久久久久亚洲精品| 日韩美女18网站久久精品| 人人狠狠综合久久88成人| 久久精品国产WWW456C0M| 国产亚洲精品自在久久| 日韩va亚洲va欧美va久久| 国产精品视频久久久| 久久精品国产99国产精品亚洲| 2021国产成人精品久久| 久久狠狠高潮亚洲精品| 国内精品伊人久久久影院| 国产AⅤ精品一区二区三区久久| 一本久久a久久精品vr综合| 久久综合久久鬼色| 国产成人综合久久精品尤物| 九九久久自然熟的香蕉图片| 色婷婷综合久久久久中文一区二区| 久久精品无码一区二区三区日韩| 国产一久久香蕉国产线看观看| 色欲久久久天天天综合网| 久久综合视频网| 国产精品久久久久久久久软件 | 国产一级持黄大片99久久 | 久久国产精品99精品国产| 精品久久久久久久久免费影院| 久久久无码精品亚洲日韩蜜臀浪潮 | 亚洲国产成人乱码精品女人久久久不卡 | 亚洲午夜无码久久久久小说|