Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

IPv4 Integer Conversion Function

Write the shortest function to convert an IP address into its integer representation and output it as an integer.

To change an IPv4 address to its integer representation, the following calculation is required:

  • Break the IP address into its four octets.
  • (Octet1 * 16777216) + (Octet2 * 65536) + (Octet3 * 256) + (Octet4)

Sample Input

192.168.1.1           10.10.104.36           8.8.8.8

Sample Output

3232235777            168454180              134744072

Answer*

Cancel
10
  • \$\begingroup\$ very clever use of main() :) .. my version was 116bytes. \$\endgroup\$
    akira
    –  akira
    2011-02-15 14:21:47 +00:00
    Commented Feb 15, 2011 at 14:21
  • \$\begingroup\$ I get a segmentation fault. \$\endgroup\$
    Nathan Osman
    –  Nathan Osman
    2011-02-19 03:11:44 +00:00
    Commented Feb 19, 2011 at 3:11
  • \$\begingroup\$ @George, what is your input and how are you running it? \$\endgroup\$
    Nim
    –  Nim
    2011-02-21 10:43:44 +00:00
    Commented Feb 21, 2011 at 10:43
  • \$\begingroup\$ I'm running it with my UserScript through codepad.org \$\endgroup\$
    Nathan Osman
    –  Nathan Osman
    2011-02-21 19:33:55 +00:00
    Commented Feb 21, 2011 at 19:33
  • \$\begingroup\$ This won't work in C++, you can't call main recursively. \$\endgroup\$
    Scott Logan
    –  Scott Logan
    2012-02-26 17:40:07 +00:00
    Commented Feb 26, 2012 at 17:40

Morty Proxy This is a proxified and sanitized view of the page, visit original site.