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
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Convert Qstring to char* and back

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 6.1k 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.
  • 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