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. [SOLVED] Convert Qstring to char* and back
QtWS25 Last Chance

[SOLVED] Convert Qstring to char* and back

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 6.0k Views
  • 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.
  • saeedhardanS Offline
    saeedhardanS Offline
    saeedhardan
    wrote on last edited by
    #1

    why is it so hard to do so ?
    !http://i57.tinypic.com/2yynk9i.png(example)!

    i tried also :
    1 - str.toStdString().c_str(); but returns const char .
    2 - str.toLocal8Bit().data(); (same trash as utf8 see below )

    in the image you see ip which qstring (1.1.1.1), in function ipValid() i convert it to char* str .
    you can see in the value of str to the right of the image (debugging mode) ,which is trash .

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Not sure what sure what you are trying to achieve. Here is simple one based on your question.

      QString data("1.1.1.1");
      qDebug() << data;
      char *datachar = data.toLocal8Bit().data();
      qDebug() << datachar;
      
      qDebug() << QString(datachar);
      

      If you want break the string based on "." you can use QString.split as well.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

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

        Hi,

        To add to Dheerendra, if you really want to validate an IP address, you should rather use a regular expression.

        If it comes from a user through a QLineEdit, you can also use a validator to ensure the user is only entering correct data.

        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
        0
        • saeedhardanS Offline
          saeedhardanS Offline
          saeedhardan
          wrote on last edited by
          #4

          thanks for tips on validating ip , but this isn't the only place i need to convert qstring to char* .

          [quote author="Dheerendra" date="1408982078"]Not sure what sure what you are trying to achieve. Here is simple one based on your question.

          char *datachar = data.toLocal8Bit().data();
          

          [/quote]

          i tried this method , but it doen't convert right , when i do :
          char* str = ip.toLocal8Bit().data(); (ip=1.1.1.1)
          str isn't 1.1.1.1 , see picture value of str .

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            Are you taking about Quotes ? which are sorounding the string ? These quotes are not part of string.

            Here is the output of my program.

            "1.1.1.1"
            1.1.1.1
            "1.1.1.1"

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            0
            • saeedhardanS Offline
              saeedhardanS Offline
              saeedhardan
              wrote on last edited by
              #6

              this is the output on my machine :
              !http://i58.tinypic.com/j7f7d3.png(output)!

              is there something wrong with my qt ? i dont understand im new to qt so sorry if its a silly mistake

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                which platform ?

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                0
                • saeedhardanS Offline
                  saeedhardanS Offline
                  saeedhardan
                  wrote on last edited by
                  #8

                  windows 8.1 32bit

                  Qt Creator 3.1.2 Based on Qt 5.3.1 (MSVC 2010, 32 bit)

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    cincirin
                    wrote on last edited by
                    #9

                    QString::toLocal8Bit::data construct a temporary QByteArray object which will be destroyed and hence you got "bullshit" datachar contents.
                    QByteArray ba = data.toLocal8Bit();
                    char* datachar = ba.data();
                    In this way ba object will be destroyed on function exit

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andreyc
                      wrote on last edited by
                      #10

                      I think qDebug() tries to be smart.
                      Try to use std::cout instead.

                      1 Reply Last reply
                      0
                      • saeedhardanS Offline
                        saeedhardanS Offline
                        saeedhardan
                        wrote on last edited by
                        #11

                        [quote author="cincirin" date="1408986595"]QString::toLocal8Bit::data construct a temporary QByteArray object which will be destroyed and hence you got "bullshit" datachar contents.
                        QByteArray ba = data.toLocal8Bit();
                        char* datachar = ba.data();
                        In this way ba object will be destroyed on function exit[/quote]

                        thank you very much sir , you r right

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andreyc
                          wrote on last edited by
                          #12

                          @
                          void barStd()
                          {
                          QString ip = "1.1.1.1";
                          qDebug() << "ip =" << ip;
                          char* localdata = ip.toLocal8Bit().data();
                          std::cout << "std::cout char* localdata = " << localdata << std::endl;
                          }

                          void barqDebug()
                          {
                          QString ip = "1.1.1.1";
                          qDebug() << "ip =" << ip;
                          char* localdata = ip.toLocal8Bit().data();
                          qDebug() << "qDebug() char* localdata = " << localdata;
                          }
                          @

                          and output
                          @
                          ip = "1.1.1.1"
                          qDebug() char* localdata =
                          ip = "1.1.1.1"
                          std::cout char* localdata = 1.1.1.1
                          @

                          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