Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Convert CString

Convert CString

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
5 Posts 4 Posters 0 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.
  • R Offline
    R Offline
    RedDragon2k
    wrote on last edited by
    #1

    Hi Guys I have to convert a IP-Address which is a CString to an UInt, but i have no clue how to do this. Also i must convert a Port CString to an UInt. MFG RedDragon2kx Unix and C are the ultimate computer viruses.

    T B 2 Replies Last reply
    0
    • R RedDragon2k

      Hi Guys I have to convert a IP-Address which is a CString to an UInt, but i have no clue how to do this. Also i must convert a Port CString to an UInt. MFG RedDragon2kx Unix and C are the ultimate computer viruses.

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      don't you use a class that use ip addresses, and in which some function take a string (either char* or std::string or CString...) ? else, you might write your own function that test first the validity of the address, and then, extract it to a UInt...


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      1 Reply Last reply
      0
      • R RedDragon2k

        Hi Guys I have to convert a IP-Address which is a CString to an UInt, but i have no clue how to do this. Also i must convert a Port CString to an UInt. MFG RedDragon2kx Unix and C are the ultimate computer viruses.

        B Offline
        B Offline
        Bob Ciora
        wrote on last edited by
        #3

        You can sscanf the string like so:

        CString sIPAddr;
        UINT nByte1, nByte2, nByte3, nByte4;
        UINT nPort;
        DWORD dwIPAddr;
        int nArgs = sscanf( (LPCTSTR)sIPAddr, "%d.%d.%d.%d:%d",
        &nByte1, &nByte2, &nByte3, &nByte4, &nPort );
        if( nArgs != 5 )
        {
        // Didn't extract 5 valid numeric arguments...take action!
        }
        else
        {
        // Validate that the nByte are valid 0-255.
        ...
        ...
        // Pack the IPAddress into the DWORD.
        dwIPAddr = (nByte1 << 24) + (nByte2 << 16) + (nByte3 << 8) + nByte4;
        }

        You may want to verify my byte ordering (nByte4 may be the high byte) when building the dwIPAddr, but this should do the trick. Bob Ciora

        R 1 Reply Last reply
        0
        • B Bob Ciora

          You can sscanf the string like so:

          CString sIPAddr;
          UINT nByte1, nByte2, nByte3, nByte4;
          UINT nPort;
          DWORD dwIPAddr;
          int nArgs = sscanf( (LPCTSTR)sIPAddr, "%d.%d.%d.%d:%d",
          &nByte1, &nByte2, &nByte3, &nByte4, &nPort );
          if( nArgs != 5 )
          {
          // Didn't extract 5 valid numeric arguments...take action!
          }
          else
          {
          // Validate that the nByte are valid 0-255.
          ...
          ...
          // Pack the IPAddress into the DWORD.
          dwIPAddr = (nByte1 << 24) + (nByte2 << 16) + (nByte3 << 8) + nByte4;
          }

          You may want to verify my byte ordering (nByte4 may be the high byte) when building the dwIPAddr, but this should do the trick. Bob Ciora

          R Offline
          R Offline
          RedDragon2k
          wrote on last edited by
          #4

          Thx. That fixed my problem. :doh: ;) Unix and C are the ultimate computer viruses.

          D 1 Reply Last reply
          0
          • R RedDragon2k

            Thx. That fixed my problem. :doh: ;) Unix and C are the ultimate computer viruses.

            D Offline
            D Offline
            digwizfox
            wrote on last edited by
            #5

            For future reference, take a look at the inet_addr function in the MSDN library. I can't believe there isn't built in global c runtime functions for parsing an IP address in this day and age. But if you are programming with MFC or windows, you can use this windows function. I hate the sscanf method myself. It seems wonderful on paper but there seem to be all kinds of weird issues when I try to use it. It never worked well for me as a validation tool for any kind of number. I always use the cruntime methods such as strtod for taking strings and validating them as actual numbers and converting them into an int value (for instance). Unfortunately, I don't see a c-run time method for IP addresses that would be more portable. Since I am a windows programmer, I have to settle for the windows libraries to provide this capability. Shawn

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups