Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I am mixed up with std::string, std::wstring, QString, anscii, utf8, unicode and charset
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
15 Posts 6 Posters 13.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    szuzsq
    wrote on 11 Oct 2011, 09:38 last edited by
    #1

    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);
    @

    1 Reply Last reply
    0
    • L Offline
      L Offline
      luisvaldes88
      wrote on 11 Oct 2011, 10:24 last edited by
      #2

      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

      Muchos quieren, pocos pueden, algunos consiguen.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on 11 Oct 2011, 10:35 last edited by
        #3

        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?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cincirin
          wrote on 11 Oct 2011, 10:42 last edited by
          #4

          [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

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fluca1978
            wrote on 11 Oct 2011, 11:08 last edited by
            #5

            Ops...I didn't notice it!

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on 11 Oct 2011, 11:14 last edited by
              #6

              [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 :-) )

              1 Reply Last reply
              0
              • S Offline
                S Offline
                szuzsq
                wrote on 12 Oct 2011, 04:25 last edited by
                #7

                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();
                }
                @

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  szuzsq
                  wrote on 12 Oct 2011, 06:03 last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    cincirin
                    wrote on 12 Oct 2011, 07:22 last edited by
                    #9

                    [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 ?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      szuzsq
                      wrote on 12 Oct 2011, 09:36 last edited by
                      #10

                      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="他" />
                      @

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        szuzsq
                        wrote on 12 Oct 2011, 09:47 last edited by
                        #11

                        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?

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          cincirin
                          wrote on 12 Oct 2011, 09:48 last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            szuzsq
                            wrote on 12 Oct 2011, 10:15 last edited by
                            #13

                            [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....

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              Franzk
                              wrote on 12 Oct 2011, 10:23 last edited by
                              #14

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

                              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                giesbert
                                wrote on 12 Oct 2011, 11:18 last edited by
                                #15

                                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.

                                Nokia Certified Qt Specialist.
                                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                1 Reply Last reply
                                0

                                1/15

                                11 Oct 2011, 09:38

                                • Login

                                • Login or register to search.
                                1 out of 15
                                • First post
                                  1/15
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved