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. Conver QString to char
Qt 6.11 is out! See what's new in the release blog

Conver QString to char

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 6.0k Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    One way to do that:

    QByteArray utf8Data = myQString.toUtf8();
    const char *string = utf8Data.constData();
    

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #3

      What is the advantage compared to: toStdString().c_str(); ?

      J 1 Reply Last reply
      0
      • ? A Former User

        What is the advantage compared to: toStdString().c_str(); ?

        J Offline
        J Offline
        Jeronimo
        wrote on last edited by
        #4

        @Q139 toStdString().c_str(); Where i put the QString here xD

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jeronimo
          wrote on last edited by
          #5

          i'm trying with char not char *something

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #6

            Char only hold 1 character, to hold more need multiple
            QString qstr;
            qstr.toStdString().c_str();

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Sorry, so used to the char * use case that I didn't thought about that one.

              So something like char myChar = myString[0].toLatin1() ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              0
              • SGaistS SGaist

                Sorry, so used to the char * use case that I didn't thought about that one.

                So something like char myChar = myString[0].toLatin1() ?

                J Offline
                J Offline
                Jeronimo
                wrote on last edited by
                #8

                @SGaist thx a lot for your help. it's possible to convert char for utf16??

                Paul ColbyP 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Not directly, utf-16 is at least 2 bytes, char is 1.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Not directly, utf-16 is at least 2 bytes, char is 1.

                    J Offline
                    J Offline
                    Jeronimo
                    wrote on last edited by
                    #10

                    @SGaist Ok so for example i dont want put char like utf16 but do the conversion for example in my if(conver UTF16(char)) it's possible? thx a lot!

                    1 Reply Last reply
                    0
                    • J Jeronimo

                      @SGaist thx a lot for your help. it's possible to convert char for utf16??

                      Paul ColbyP Offline
                      Paul ColbyP Offline
                      Paul Colby
                      wrote on last edited by Paul Colby
                      #11

                      @Jeronimo said in Conver QString to char:

                      @SGaist thx a lot for your help. it's possible to convert char for utf16??

                      As @SGaist said, not with char. You'd need to use a wide char instead, such as:

                      wchar_t myChar = myString[0].unicode();
                      

                      However, wchar_t is compiler / platform dependant, and not portable when used in this way.

                      What would you want to pass the UTF-16 character on to? QChar::unicode() would probably work for that case.

                      Cheers.

                      J 1 Reply Last reply
                      3
                      • Paul ColbyP Paul Colby

                        @Jeronimo said in Conver QString to char:

                        @SGaist thx a lot for your help. it's possible to convert char for utf16??

                        As @SGaist said, not with char. You'd need to use a wide char instead, such as:

                        wchar_t myChar = myString[0].unicode();
                        

                        However, wchar_t is compiler / platform dependant, and not portable when used in this way.

                        What would you want to pass the UTF-16 character on to? QChar::unicode() would probably work for that case.

                        Cheers.

                        J Offline
                        J Offline
                        Jeronimo
                        wrote on last edited by Jeronimo
                        #12

                        @Paul-Colby said in Conver QString to char:

                        QChar::unicode()

                        solved i prefer this method:

                        QString cadena = "Hola";
                        std::string cadenaStd = cadena.toStdString();
                        QChar c = cadenaStd[0] or char

                        1 Reply Last reply
                        1
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          Why not use QChar c = cadena[0] ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1

                          • Login

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