I am mixed up with std::string, std::wstring, QString, anscii, utf8, unicode and charset



  • In windows, I have a class to converter charset between anscii, utf8, unicode.
    But, I can not transfer it to mac or linux, Is Any one can help me?
    @
    #pragma once

    #include <string>
    #ifdef WIN32
    #include <wtypes.h>
    #endif

    class CStringConverter {
    public:
    static std::wstring Ansi2Unicode(std::string szAnsi) {
    #ifdef WIN32
    //calc block size to be returned
    int len = MultiByteToWideChar(CP_ACP, NULL, szAnsi.c_str(), szAnsi.size(), NULL, 0);
    //malloc and fill the returned block
    wchar_t* szUnicode = new wchar_t[len+1];
    MultiByteToWideChar(CP_ACP, NULL, szAnsi.c_str(), szAnsi.size(), szUnicode, len);
    szUnicode[len] = 0;
    std::wstring rs = szUnicode;
    delete[] szUnicode;

        return rs;
    

    #else
    //>>?? //fixme
    return L"";
    #endif
    }
    static std::string Unicode2Ansi(std::wstring szUnicode) {
    #ifdef WIN32
    //calc block size to be returned
    int len = WideCharToMultiByte(CP_ACP, NULL, szUnicode.c_str(), szUnicode.size(), NULL, 0, NULL, NULL);
    //malloc and fill the returned block
    char* szAnsi = new char[len + 1];
    WideCharToMultiByte(CP_ACP, NULL, szUnicode.c_str(), szUnicode.size(), szAnsi, len, NULL, NULL);
    szAnsi[len] = 0;
    std::string rs = szAnsi;
    delete[] szAnsi;

        return rs;
    

    #else
    //>>?? //fixme
    return "";
    #endif
    }

    static std::wstring Utf82Unicode(std::string szUtf8) {
    

    #ifdef WIN32
    //calc block size to be returned
    int len = MultiByteToWideChar(CP_UTF8, NULL, szUtf8.c_str(), szUtf8.size(), NULL, 0);
    //malloc and fill the returned block
    wchar_t* szUnicode = new wchar_t[len+1];
    MultiByteToWideChar(CP_UTF8, NULL, szUtf8.c_str(), szUtf8.size(), szUnicode, len);
    szUnicode[len] = 0;
    std::wstring rs = szUnicode;
    delete[] szUnicode;

        return rs;
    

    #else
    //>>?? //fixme
    return L"";
    #endif
    }
    static std::string Unicode2Utf8(std::wstring szUnicode) {
    #ifdef WIN32
    //calc block size to be returned
    int len = WideCharToMultiByte(CP_UTF8, NULL, szUnicode.c_str(), szUnicode.size(), NULL, 0, NULL, NULL);
    //malloc and fill the returned block
    char* szUtf8 = new char[len + 1];
    WideCharToMultiByte(CP_UTF8, NULL, szUnicode.c_str(), szUnicode.size(), szUtf8, len, NULL, NULL);
    szUtf8[len] = 0;
    std::string rs = szUtf8;
    delete[] szUtf8;

        return rs;
    

    #else
    //>>?? //fixme
    return "";
    #endif
    }

    static std::string Ansi2Utf8(std::string szAnsi) {
        return Unicode2Utf8(Ansi2Unicode(szAnsi));
    }
    static std::string Utf82Ansi(std::string szUtf8) {
        return Unicode2Ansi(Utf82Unicode(szUtf8));
    }
    

    };
    @


    @
    //demo
    std::string szAnsi = "abc123你我他";

    std::wstring szUnicode = CStringConverter::Ansi2Unicode(szAnsi);
    szAnsi = CStringConverter::Unicode2Ansi(szUnicode);

    std::string szUft8 = CStringConverter::Unicode2Utf8(szUnicode);
    szUnicode = CStringConverter::Utf82Unicode(szUft8);

    szAnsi = CStringConverter::Utf82Ansi(szUft8);
    szUft8 = CStringConverter::Ansi2Utf8(szAnsi);
    @



  • hi, very cool...

    I thing QString support everthing you are trying to do. I'm not 100% sure.
    did you take a look at QString documentation?

    best regards



  • The only conversion that seems to me not possible is from std::wstring, I don't see any constructor of QString with this signature, but all other conversions seems available to me.
    Anyway, what is the problem you are having converting your class?



  • [quote author="fluca1978" date="1318329336"]The only conversion that seems to me not possible is from std::wstring[/quote] It is "fromStdWString":http://doc.qt.nokia.com/latest/qstring.html#fromStdWString



  • Ops...I didn't notice it!



  • [quote author="fluca1978" date="1318331294"]Ops...I didn't notice it![/quote]
    No problem :-)
    Qt framework is big enough for someone to know everything ... ( at least I think so :-) )



  • My new implement is like this:
    @
    #pragma once

    #include <string>
    #include <QString>

    class CStringConverter {
    public:
    static std::wstring Ansi2Unicode(std::string szAnsi) {
    QString s = QString::fromAscii(szAnsi.c_str());
    return s.toStdWString();
    }
    static std::string Unicode2Ansi(std::wstring szUnicode) {
    QString s = QString::fromStdWString(szUnicode);
    return s.toAscii();
    }

    static std::wstring Utf82Unicode(std::string szUtf8) {
        QString s = QString::fromUtf8(szUtf8.c_str());
        return s.toStdWString();
    }
    static std::string Unicode2Utf8(std::wstring szUnicode) {
        QString s = QString::fromStdWString(szUnicode);
        return s.toUtf8();
    }
    
    static std::string Ansi2Utf8(std::string szAnsi) {
        return Unicode2Utf8(Ansi2Unicode(szAnsi));
    }
    static std::string Utf82Ansi(std::string szUtf8) {
        return Unicode2Ansi(Utf82Unicode(szUtf8));
    }
    

    };
    @


    But there are still problem.
    1).it can be compiled using qt under windows, but can not be compiled under mac?
    @
    static std::string Unicode2Ansi(std::wstring szUnicode) {
    QString s = QString::fromStdWString(szUnicode);
    //in this statement, the error is:
    //converstion from 'QByteArray' to non-scalar type 'std::string' requested
    return s.toAscii();
    }
    @



  • BTW,I try to create a "Qt Console Application" in Mac
    @
    #untitiled2.pro
    QT += core

    QT -= gui

    DEFINES += UNICODE

    TARGET = untitled2
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp

    @

    @
    //main.cpp
    #include <QtCore/QCoreApplication>

    #include<string>
    #include<QString>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    std::string stdStr = "abc123你我他_stdStr";
    std::wstring stdWStr = L"abc123你我他_stdWStr";
    
    QString qStr="abc123你我他_qStr";
    
    printf(stdStr.c_str());
    printf("\n");
    
    wprintf(stdWStr.c_str());
    printf("\n");
    
    wprintf(qStr.toStdWString().c_str());
    printf("\n");
    
    
    return a.exec(&#41;;
    

    }
    @

    when I run in debug mode, I add a breakpoints before
    @
    return a.exec();
    @
    and open "Locals and Expressions" window,
    but in that window, there are not any string value.
    the table in "Locals and Expressions" window is:
    @
    Name Value Type

    qStr QString
    stdStr string
    stdWStr wstring
    @

    the "value" field are null.
    I may want to view at the "value" field: "abc123你我他_stdStr","abc123你我他_stdWStr","abc123你我他_qStr", for the std::string,std::wstring,QString variable value.

    Is any way for me to watch the std::string, std::wstring, QString variable value, when I debug?


    And when I watch X11 console,it print as:

    abc123ä½ æä»_stdStr
    abc123_stdWStr
    abc123ä½ æä»_qStr

    maybe, it look like don't support chinese or japanese language.

    How can I make X11 console to print exactly?
    as:
    abc123你我他_stdStr
    abc123你我他_stdWStr
    abc123你我他_qStr

    but this program run under window.
    the table in "Locals and Expressions" window is:
    @
    Name Value Type

    qStr "abc123ÄãÎÒËû_qStr" QString
    stdStr 0x3f9da0 "abc123???" string
    stdWStr class std::basic_string<> wstring
    @


    And the console print as:

    abc123你我他stdStr
    abc123
    abc123你我他_qStr



  • [quote author="szuzsq" date="1318393533"]
    1).it can be compiled using qt under windows, but can not be compiled under mac?
    @
    static std::string Unicode2Ansi(std::wstring szUnicode) {
    QString s = QString::fromStdWString(szUnicode);
    //in this statement, the error is:
    //converstion from 'QByteArray' to non-scalar type 'std::string' requested
    return s.toAscii();
    }
    @[/quote]

    Your "Unicode2Ansi" function involve calling "QByteArray::operator const char * ()":http://doc.qt.nokia.com/latest/qbytearray.html#operator-const-char--2a Maybe on Mac QT_NO_CAST_FROM_BYTEARRAY is implicity defined.
    Why you do not call directly "QString::toStdString":http://doc.qt.nokia.com/latest/qstring.html#toStdString ?



  • Because of I using 2 different sdk, and someone using char*, and someone using wchat_t*.
    so I must convert between them.

    In the last demo, I use std::string, QString, std::wstring(convert to std::string), to write a simple xml file.
    @
    #include "tinyxml.h"

    #include <string>
    #include <QString>

    int main(int argc, char* argv[]) {
    TiXmlDocument doc;
    TiXmlElement elem("rene");

    //directly using std::string and QString
    std::string s = "_你_";
    elem.SetAttribute("s", s.c_str());
    
    QString qs = "_我_";
    elem.SetAttribute("qs",qs.toStdString().c_str());
    
    //convert std::wstring to std::string
    //using by QString for adapter
    std::wstring ws = L"_他_";
    QString qs2 = QString::fromStdWString(ws);
    std::string s2 = qs2.toStdString();
    elem.SetAttribute("ws", s2.c_str());
    
    doc.InsertEndChild(elem);
    
    doc.SaveFile&#40;"0.xml"&#41;;
    

    }
    @

    but the result xml file have some error:
    @
    <rene s="" qs="" ws="?" />
    @

    and I look forward to the right xml file like:
    @
    <rene s="" qs="" ws="" />
    @



  • when I invoke
    @
    char* rs = setlocale(LC_CTYPE, "");
    @

    in windows, the result is: "Chinese_People's Repulic of China 936";
    but in mac, the result is :"C".

    Is there any wrong?



  • Well, then have a look at "convert a QString to char*":http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa using locale settings.



  • [quote author="cincirin" date="1318412939"]Well, then have a look at "convert a QString to char*":http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa using locale settings.[/quote]

    thank you, but the wrong is also exist.

    in fact, if I use QString::fromStdString, fromStdWString, toStdString, toStdWString to convert between std::string and std::wstring,
    with latin or ascii word, it run ok, not wrong;
    but with chinese or japanese word, it run wrong....



  • Note that your compiler may actually have trouble reading non-ascii characters from your file.



  • from/toStdString always converts the text. If you want to be safe, you MUST know, which char* convention you use. Codepage based? Utf-8?

    I personally prefer utf-8, which is simple with QString:

    @
    QByteArray ba = string.toUtf8();
    @

    The byte array has the correct length and a const char* conversion so you can put it to an std::string.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.