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.6k 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.
  • F Offline
    F Offline
    fluca1978
    wrote on 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 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 last edited by
        #5

        Ops...I didn't notice it!

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cincirin
          wrote on 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 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 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 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 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 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 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 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 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 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

                            • Login

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