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. Qt5 convert QString into char [] or char *
Forum Updated to NodeBB v4.3 + New Features

Qt5 convert QString into char [] or char *

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 25.7k Views 4 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.
  • yczoY yczo

    Hello, I googled a lot over that theme, all deprecated or do not work

    QString hello = "Hello World"; ---> to char *cHello or to char cHello[30]

    How to convert in Qt5 to a standard c++ char array?

    i tried for(int i = 0; ;i++) cHello[i] = hello.at(i); .... and much more, but without sucess

    Someone kind enough to help? please help :-)

    thanks in advance. greetings

    AggsA Offline
    AggsA Offline
    Aggs
    wrote on last edited by
    #2

    @yczo hello.ToStdString().c_str()

    1 Reply Last reply
    1
    • yczoY Offline
      yczoY Offline
      yczo
      wrote on last edited by yczo
      #3

      Thank you very much for answer, but i think that this is deprecated method. :-(
      When I run
      char *cHello = hello.ToStdString().c_str();

      get the next error:

      error: C2039: 'ToStdString' : is not a member of 'QString'

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

        Just a typo. It's std::string QString::toStdString() const.

        1 Reply Last reply
        2
        • sneubertS Offline
          sneubertS Offline
          sneubert
          wrote on last edited by
          #5

          hi yczo,
          try hello.toStdString().c_str();
          mind the small letter

          http://doc.qt.io/qt-5/qstring.html#toStdString

          1 Reply Last reply
          0
          • yczoY Offline
            yczoY Offline
            yczo
            wrote on last edited by yczo
            #6

            but... there is not a way to direct obtain a char array instead of a string? the question was over an char array hehee ;-)

            char *cHola =

            Thank you very much (I appreciate the kindness)...

            mrjjM 1 Reply Last reply
            0
            • sneubertS Offline
              sneubertS Offline
              sneubert
              wrote on last edited by
              #7

              you are right, the pointer you get is const, but you can do something like

              char cHola[512] = {0};
              std::copy(hello.toStdString().begin(),hello.toStdString().end(),cHola);
              

              but remind that there´s no boundary check on cHola and you get nonsens if it´s not a basic_string<char>

              1 Reply Last reply
              1
              • yczoY yczo

                but... there is not a way to direct obtain a char array instead of a string? the question was over an char array hehee ;-)

                char *cHola =

                Thank you very much (I appreciate the kindness)...

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @yczo
                hi
                Nope
                QString dont convert directly to char *.

                But can I ask why u need char * ?

                QString class can do anything u can do with char * and in better and more safe way.

                1 Reply Last reply
                0
                • yczoY Offline
                  yczoY Offline
                  yczo
                  wrote on last edited by yczo
                  #9

                  Hello and thank you; I need "char *hola" or "char hola[maxSize]" for send simply chars to serial port through Windows handler

                  Greetings

                  kshegunovK 1 Reply Last reply
                  0
                  • sneubertS Offline
                    sneubertS Offline
                    sneubert
                    wrote on last edited by sneubert
                    #10

                    so the answer is already there, something like
                    WriteFile(yourComHandle, hello.toStdString().c_str(), hello.length(), &dw_bytesWritten, NULL)
                    should do the job

                    1 Reply Last reply
                    1
                    • yczoY yczo

                      Hello and thank you; I need "char *hola" or "char hola[maxSize]" for send simply chars to serial port through Windows handler

                      Greetings

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #11

                      @yczo said:

                      I need "char *hola" or "char hola[maxSize]" for send simply chars to serial port through Windows handler

                      You're better of using QSerialPort (which accepts QByteArray) and respecting the string's encoding. Also referencing temporaries can be inconspicuously dangerous in some cases. Consider this (modified from @sneubert's example):

                      char * string = hello.toStdString().c_str(); // Now we get a dangling pointer to a location we don't own!
                      WriteFile(yourComHandle, string, hello.length(), &dw_bytesWritten, NULL); // Might work, depending on the compiler, might get a segfault
                      

                      How about this, doesn't it look better:

                      QSerialPort port;
                      // ... init and so on ...
                      
                      QByteArray data = hello.toLatin1(); // Enforce latin1 encoding (or utf8, or local 8-bit or whatever your device is expecting)
                      port.write(data);
                      port.waitForBytesWritten(1000); // Only for illustration, you can (and should) use the async API
                      // ... and so on ...
                      

                      Or, if you really insist on using char *, then enforce the encoding to a byte-array and then get the char pointer from there:

                      QByteArray string = hello.toLatin1();
                      char * strdata = string.data(); // This pointer is valid during the lifetime of the QByteArray instance (provided the byte array is not changed in the meantime)
                      

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      2

                      • Login

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