Ip Octed string To dec or IP class

Discussion in 'NZ Computing' started by Warwick, Jan 18, 2005.

  1. Warwick

    Warwick Guest

    I wonder if anyone has a function that returns a decimal value from an ip#
    string. Or even better, an IP class with overloaded comparison operators.

    I need one for this poll/survey thing, and I'd rather not have to write
    one.


    Any ideas?
    The name of an open source program that uses one would do.


    cheers
    Warwick, Jan 18, 2005
    #1
    1. Advertising

  2. Warwick

    Allistar Guest

    Warwick wrote:

    > I wonder if anyone has a function that returns a decimal value from an ip#
    > string. Or even better, an IP class with overloaded comparison operators.
    >
    > I need one for this poll/survey thing, and I'd rather not have to write
    > one.
    >
    >
    > Any ideas?
    > The name of an open source program that uses one would do.
    >
    >
    > cheers


    Couldn't you do something like (in C):

    int segment1=192, segment2=168, segment3=0, segment4=3, result;
    result = (segment1 << 24) + (segment2 << 16) + (segment3 << 8) + segment4


    (getting the 4 sgements from a string like "192.168.0.3" is trivial and left
    as an exercise for the reader :)

    Allistar.
    Allistar, Jan 18, 2005
    #2
    1. Advertising

  3. Warwick

    Warwick Guest

    On Tue, 18 Jan 2005 19:05:57 +1300, Allistar wrote:

    > Warwick wrote:
    >
    >> I wonder if anyone has a function that returns a decimal value from an ip#
    >> string. Or even better, an IP class with overloaded comparison operators.
    >>
    >> I need one for this poll/survey thing, and I'd rather not have to write
    >> one.
    >>
    >>
    >> Any ideas?
    >> The name of an open source program that uses one would do.
    >>
    >>
    >> cheers

    >
    > Couldn't you do something like (in C):
    >
    > int segment1=192, segment2=168, segment3=0, segment4=3, result;
    > result = (segment1 << 24) + (segment2 << 16) + (segment3 << 8) + segment4
    >
    >
    > (getting the 4 sgements from a string like "192.168.0.3" is trivial and left
    > as an exercise for the reader :)
    >
    > Allistar.



    Needs an OctalStringToInt function as well doesn't it?
    I don't think I have one of those in the lib, I'll have to write it.

    I might make a class with overloaded comparison operators that takes a
    string as an argument in a constructor.


    The strings I am getting out of the NNTP-Posting-Host field from a usenet
    posts header are crappy. Clear ones seem to be delimited with dashes
    instead of dots, and plenty of them have crap appended to the end.


    I am formulating an as yet unwritten rule, trivial programming exercises
    often turn into significant headaches.

    Thanks for the advice.

    cheers
    Warwick, Jan 18, 2005
    #3
  4. Warwick

    Enkidu Guest

    On Tue, 18 Jan 2005 20:28:54 +1300, Warwick <> wrote:

    >On Tue, 18 Jan 2005 19:05:57 +1300, Allistar wrote:
    >
    >> Warwick wrote:
    >>
    >>> I wonder if anyone has a function that returns a decimal value from an ip#
    >>> string. Or even better, an IP class with overloaded comparison operators.
    >>>
    >>> I need one for this poll/survey thing, and I'd rather not have to write
    >>> one.
    >>>
    >>>
    >>> Any ideas?
    >>> The name of an open source program that uses one would do.
    >>>
    >>>
    >>> cheers

    >>
    >> Couldn't you do something like (in C):
    >>
    >> int segment1=192, segment2=168, segment3=0, segment4=3, result;
    >> result = (segment1 << 24) + (segment2 << 16) + (segment3 << 8) + segment4
    >>
    >>
    >> (getting the 4 sgements from a string like "192.168.0.3" is trivial and left
    >> as an exercise for the reader :)
    >>
    >> Allistar.

    >
    >
    >Needs an OctalStringToInt function as well doesn't it?
    >I don't think I have one of those in the lib, I'll have to write it.
    >

    No, an IP address octet is normally given as a decimal number from
    zero to 255. Just split the string at the periods, then

    dec = (((((octet1 * 256) + octet2) * 256) + octet3) * 256) + octet4

    Cheers,

    Cliff
    --

    The National Party manifesto can be viewed here:

    http://www.labour.org.nz/policy/index.html
    Enkidu, Jan 18, 2005
    #4
  5. Warwick

    Warwick Guest

    On Tue, 18 Jan 2005 22:27:15 +1300, Enkidu wrote:

    > On Tue, 18 Jan 2005 20:28:54 +1300, Warwick <> wrote:
    >
    >>On Tue, 18 Jan 2005 19:05:57 +1300, Allistar wrote:
    >>
    >>> Warwick wrote:
    >>>
    >>>> I wonder if anyone has a function that returns a decimal value from an ip#
    >>>> string. Or even better, an IP class with overloaded comparison operators.
    >>>>
    >>>> I need one for this poll/survey thing, and I'd rather not have to write
    >>>> one.
    >>>>
    >>>>
    >>>> Any ideas?
    >>>> The name of an open source program that uses one would do.
    >>>>
    >>>>
    >>>> cheers
    >>>
    >>> Couldn't you do something like (in C):
    >>>
    >>> int segment1=192, segment2=168, segment3=0, segment4=3, result;
    >>> result = (segment1 << 24) + (segment2 << 16) + (segment3 << 8) + segment4
    >>>
    >>>
    >>> (getting the 4 sgements from a string like "192.168.0.3" is trivial and left
    >>> as an exercise for the reader :)
    >>>
    >>> Allistar.

    >>
    >>
    >>Needs an OctalStringToInt function as well doesn't it?
    >>I don't think I have one of those in the lib, I'll have to write it.
    >>

    > No, an IP address octet is normally given as a decimal number from
    > zero to 255. Just split the string at the periods, then
    >
    > dec = (((((octet1 * 256) + octet2) * 256) + octet3) * 256) + octet4
    >
    > Cheers,
    >
    > Cliff


    Hell, I should have known that as soon as I saw the 9 in 192 lol.

    thanks :)
    Warwick, Jan 18, 2005
    #5
  6. Warwick

    Ralph Fox Guest

    On Tue, 18 Jan 2005 20:28:54 +1300, in message
    <zwwsvtqmcprv$.q04x17ilomr9$>, Warwick wrote:

    > The strings I am getting out of the NNTP-Posting-Host field from a usenet
    > posts header are crappy. Clear ones seem to be delimited with dashes
    > instead of dots, and plenty of them have crap appended to the end.


    That's because they are _not_ IP addresses (like "210.246.1.48"),
    but are fully-qualified domain names (like "dialup-48-1-246-210.isp.net"
    or "pppf6-1-48.isp.com" or "210-246-1-48.paradise.net.nz").

    You can't be sure that any numbers you see in a FQDN are the
    parts of the IP address in correct order. That depends on what
    the poster's ISP provides for reverse-DNS.

    The only sure way to convert a FQDN to an IP is to do an
    nslookup.


    --
    Cheers,
    Ralph

    "A lack of planning on your part does not constitute an emergency on mine."
    Ralph Fox, Jan 18, 2005
    #6
  7. Warwick

    Allistar Guest

    Enkidu wrote:

    > On Tue, 18 Jan 2005 20:28:54 +1300, Warwick <> wrote:
    >
    >>On Tue, 18 Jan 2005 19:05:57 +1300, Allistar wrote:
    >>
    >>> Warwick wrote:
    >>>
    >>>> I wonder if anyone has a function that returns a decimal value from an
    >>>> ip# string. Or even better, an IP class with overloaded comparison
    >>>> operators.
    >>>>
    >>>> I need one for this poll/survey thing, and I'd rather not have to write
    >>>> one.
    >>>>
    >>>>
    >>>> Any ideas?
    >>>> The name of an open source program that uses one would do.
    >>>>
    >>>>
    >>>> cheers
    >>>
    >>> Couldn't you do something like (in C):
    >>>
    >>> int segment1=192, segment2=168, segment3=0, segment4=3, result;
    >>> result = (segment1 << 24) + (segment2 << 16) + (segment3 << 8) +
    >>> segment4
    >>>
    >>>
    >>> (getting the 4 sgements from a string like "192.168.0.3" is trivial and
    >>> left as an exercise for the reader :)
    >>>
    >>> Allistar.

    >>
    >>
    >>Needs an OctalStringToInt function as well doesn't it?
    >>I don't think I have one of those in the lib, I'll have to write it.
    >>

    > No, an IP address octet is normally given as a decimal number from
    > zero to 255. Just split the string at the periods, then
    >
    > dec = (((((octet1 * 256) + octet2) * 256) + octet3) * 256) + octet4
    >
    > Cheers,
    >
    > Cliff


    That's what I said originally (although I used bit shifting rather than
    multiplicatino, which will be marginally faster (but the speed computer run
    these days you wouldn't notice unless you did this a million times over).

    Allistar.
    Allistar, Jan 18, 2005
    #7
  8. In article <>,
    Enkidu <> wrote:

    >No, an IP address octet is normally given as a decimal number from
    >zero to 255.


    I have seen spam messages that included IP address numbers in octal, and
    also specified the address as a single large integer rather than as a
    dotted-quad.
    Lawrence D'Oliveiro, Jan 24, 2005
    #8
  9. Warwick

    David Guest

    Warwick wrote:
    > I wonder if anyone has a function that returns a decimal value from an ip#
    > string. Or even better, an IP class with overloaded comparison operators.
    >
    > I need one for this poll/survey thing, and I'd rather not have to write
    > one.
    >
    >
    > Any ideas?
    > The name of an open source program that uses one would do.
    >
    >
    > cheers

    inet_addr is what you want (included in BSD and winsock)
    u_long ipaddress=inet_addr("192.168.1.2");
    David, Jan 27, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jonnah
    Replies:
    5
    Views:
    911
    Walter Roberson
    May 19, 2004
  2. Harkin
    Replies:
    0
    Views:
    494
    Harkin
    Jul 19, 2005
  3. gaurav_kant

    How String class is immutable?

    gaurav_kant, Sep 26, 2007, in forum: Software
    Replies:
    0
    Views:
    982
    gaurav_kant
    Sep 26, 2007
  4. newasp.net
    Replies:
    1
    Views:
    1,604
    imransyed63
    Aug 21, 2008
  5. Meat Plow

    Re: Class 5 vs. Class 6 Toilets

    Meat Plow, Aug 4, 2010, in forum: Computer Support
    Replies:
    0
    Views:
    645
    Meat Plow
    Aug 4, 2010
Loading...

Share This Page