Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > IP: long-> InetAddress

Reply
Thread Tools

IP: long-> InetAddress

 
 
vertigo
Guest
Posts: n/a
 
      05-02-2004
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

 
Reply With Quote
 
 
 
 
Marco Schmidt
Guest
Posts: n/a
 
      05-02-2004
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 -=
{
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      05-02-2004
On Sun, 02 May 2004 16:10:27 +0200, vertigo <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
InetAddress question Rhino Java 1 02-26-2005 04:52 PM
InetAddress isReachable() andy.dreistadt@gmail.com Java 0 02-10-2005 03:07 PM
InetAddress Linus Java 5 01-31-2005 09:25 AM
performing reverse DNS lookup with InetAddress Madhur Ahuja Java 1 08-29-2004 07:57 AM
Solaris 9 and InetAddress with IP hostname C Java 1 09-16-2003 12:27 PM



Advertisments