IP: long-> InetAddress

Discussion in 'Java' started by vertigo, May 2, 2004.

  1. vertigo

    vertigo Guest

    Hello
    i have received ip address in long and i want to convert it
    to InetAddress. For example, i have:
    167772939 and i want to receive InetAddress which point's to 10.0.3.11.

    How can i do it ?

    Thanx
    Michal
    vertigo, May 2, 2004
    #1
    1. Advertising

  2. vertigo:

    >i have received ip address in long and i want to convert it
    >to InetAddress. For example, i have:
    >167772939 and i want to receive InetAddress which point's to 10.0.3.11.


    The operations shift right (>>) and bitwise and (&) can be used to
    accomplish just that. Alternatively, division (/) and modulus (%).

    I have recently written such a method:

    public static String fromNumerical(long address)
    {
    StringBuffer sb = new StringBuffer();
    for (int i = 0, shift = 24; i < 4; i++, shift -= 8)
    {
    long value = (address >> shift) & 0xff;
    sb.append(value);
    if (i != 3)
    {
    sb.append('.');
    }
    }
    return sb.toString();
    }

    If there is something like that in the runtime library, I don't know
    it.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
    Marco Schmidt, May 2, 2004
    #2
    1. Advertising

  3. vertigo

    Roedy Green Guest

    On Sun, 02 May 2004 16:10:27 +0200, vertigo <> wrote
    or quoted :

    >i have received ip address in long and i want to convert it
    >to InetAddress. For example, i have:
    >167772939 and i want to receive InetAddress which point's to 10.0.3.11.
    >
    >How can i do it ?


    see http://mindprod.com/jgloss/ip.html


    public class DottedQuad
    {
    /**
    * display an IP as a dotted quad xxx.xxx.xxx.xxx
    */
    public static String dottedQuad ( int ip )
    {
    StringBuffer sb = new StringBuffer( 15 );
    for ( int shift=24; shift >0; shift-=8 )
    {
    // process 3 bytes, from high order byte down.
    sb.append( Integer.toString( (ip >>> shift) & 0xff ));
    sb.append('.');
    }
    sb.append(Integer.toString( ip & 0xff ));
    return sb.toString();
    }

    /**
    * test harness
    *
    * @param args not used
    */
    public static void main ( String[] args )
    {
    System.out.println( dottedQuad( 0x01ff0010 ) );
    }
    }

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 3, 2004
    #3
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    662
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,179
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    463
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    784
    Peter Nilsson
    Jan 15, 2008
  5. veryhotsausage
    Replies:
    1
    Views:
    1,779
    veryhotsausage
    Jul 4, 2008
Loading...

Share This Page