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. IP Address conversion from string to unsigned int
Forum Updated to NodeBB v4.3 + New Features

IP Address conversion from string to unsigned int

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 12.9k 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.
  • K Offline
    K Offline
    Klondike6
    wrote on 3 Mar 2012, 16:02 last edited by
    #1

    I have a QString where the user provides an IP address like "192.168.0.128" and I need to convert this to the corresponding 32-bit unsigned int value (0xC0A80080). Is there a recommended way to do this?

    Of course, I could come up with my own method to parse the string and do the conversion, but I'm sure there are many applications that do this already - I don't want to reinvent it if Qt already provides something like this.

    Thanks for any guidance you can provide.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 3 Mar 2012, 17:45 last edited by
      #2

      Use "QHostAddress::toIPv4Address":https://qt-project.org/doc/qt-4.8/qhostaddress.html#toIPv4Address

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Klondike6
        wrote on 3 Mar 2012, 18:07 last edited by
        #3

        I did try using QHostAddress, but had problems with unresolved externals, couldn't find the methods in QHostAddress - ???

        Anyway, here is an alternative approach:

        I have a QString called Server_IP containing something like "192.168.0.128". The code below converts this to a 32-bit value (serverIPnum):
        @
        QStringList server_octets = Server_IP.split(".");
        s1 = server_octets.at(0).toLong();
        s2 = server_octets.at(1).toLong();
        s3 = server_octets.at(2).toLong();
        s4 = server_octets.at(3).toLong();
        serverIPnum = (s1 << 24) | (s2 << 16) | (s3 << 8) | s4;
        @
        Maybe not very elegant, but it works.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          d2uriel
          wrote on 3 Mar 2012, 18:11 last edited by
          #4

          Have you added QtNetwork module to your project? Probably not and that's why you had unresolved external errors.

          "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on 3 Mar 2012, 18:47 last edited by
            #5

            d2uriel is probably right that you did not include QNetwork module.

            Please use "code wrappings":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 next time. I have added them for you.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Klondike6
              wrote on 3 Mar 2012, 19:40 last edited by
              #6

              I had included QtNetwork, still had the unresolved externals. Not really an issue anymore since I am using my alternative approach.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                d2uriel
                wrote on 4 Mar 2012, 08:12 last edited by
                #7

                You added that to the .PRO file and ran QMake afterwards?

                Anyways, as you say, case is closed.

                "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

                1 Reply Last reply
                0

                1/7

                3 Mar 2012, 16:02

                • Login

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