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. inet_addr function and leading zeros

inet_addr function and leading zeros

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
3 Posts 2 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.
  • J Offline
    J Offline
    JBAK_CP
    wrote on last edited by
    #1

    Hi, I'm trying to use the 'inet_addr' function to convert a char IP address, but I think since the IP Address i'm passing in to the 'inet_addr' function has leading zero's (192.169.055.075), the 'inet_addr' function is interpreting this differently. Any suggestion on how to remove the leading zeros? Thanks char IPAddr[20]; //192.169.055.075 ulAddr = inet_addr(IPAddr);

    C 1 Reply Last reply
    0
    • J JBAK_CP

      Hi, I'm trying to use the 'inet_addr' function to convert a char IP address, but I think since the IP Address i'm passing in to the 'inet_addr' function has leading zero's (192.169.055.075), the 'inet_addr' function is interpreting this differently. Any suggestion on how to remove the leading zeros? Thanks char IPAddr[20]; //192.169.055.075 ulAddr = inet_addr(IPAddr);

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      JBAK_CP wrote:

      Hi, I'm trying to use the 'inet_addr' function to convert a char IP address, but I think since the IP Address i'm passing in to the 'inet_addr' function has leading zero's (192.169.055.075), the 'inet_addr' function is interpreting this differently.

      Indeed, according to documentation, numbers with a leading zero are parsed as octal. Provided your format is fixed (as it looks) you may use

      int a[4];
      char c[20];
      if ( sscanf("192.169.055.075","%03d.%03d.%03d.%03d", a, a+1,a+2,a+3) == 4)
      {
      sprintf(c, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
      }

      eventually c[] contains the address without leading zeros. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      J 1 Reply Last reply
      0
      • C CPallini

        JBAK_CP wrote:

        Hi, I'm trying to use the 'inet_addr' function to convert a char IP address, but I think since the IP Address i'm passing in to the 'inet_addr' function has leading zero's (192.169.055.075), the 'inet_addr' function is interpreting this differently.

        Indeed, according to documentation, numbers with a leading zero are parsed as octal. Provided your format is fixed (as it looks) you may use

        int a[4];
        char c[20];
        if ( sscanf("192.169.055.075","%03d.%03d.%03d.%03d", a, a+1,a+2,a+3) == 4)
        {
        sprintf(c, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
        }

        eventually c[] contains the address without leading zeros. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        J Offline
        J Offline
        JBAK_CP
        wrote on last edited by
        #3

        Thanks! :thumbsup:

        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