5-8万年薪顶级嵌入式,京沪深就业地 浅谈并行编程中的任务分解模式
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  Web 开发

各位大侠,php中ip2long,long2ip的2个函数,在java中如何实现?100分

楼主savage711()2005-10-09 11:12:32 在 Java / Web 开发 提问

各位大侠,php中ip2long,long2ip的2个函数,在java中如何实现?100分 问题点数:100、回复次数:3Top

1 楼keiy()回复于 2005-10-09 11:32:27 得分 100

//ip2long  
   
  import   java.net.*;  
   
  public   class   test   {  
    public   static   int   str2Ip(String   ip)   throws   UnknownHostException   {    
                  InetAddress   address   =   InetAddress.getByName(ip);    
                  byte[]   bytes   =   address.getAddress();    
                  int   a,   b,   c,   d;    
                  a   =   byte2int(bytes[0]);    
                  b   =   byte2int(bytes[1]);    
                  c   =   byte2int(bytes[2]);    
                  d   =   byte2int(bytes[3]);    
                  int   result   =   (a   <<   24)   |   (b   <<   16)   |   (c   <<   8)   |   d;    
                  return   result;    
          }    
      public   static   int   byte2int(byte   b)   {    
                  int   l   =   b   &   0x07f;    
                  if   (b   <   0)   {    
                          l   |=   0x80;    
                  }    
                  return   l;    
          }    
   
   
  public   static   long   ip2long(String   ip)   throws   UnknownHostException   {    
                  int   ipNum   =   str2Ip(ip);    
                  return   int2long(ipNum);    
          }    
   
    public   static   long   int2long(int   i)   {    
                  long   l   =   i   &   0x7fffffffL;    
                  if   (i   <   0)   {    
                          l   |=   0x080000000L;    
                  }    
                  return   l;    
          }    
   
  public   static   void   main(String[]   args)   throws   Exception   {  
  System.out.println(ip2long("192.168.0.111"));  
   
  }  
   
  }  
  Top

2 楼savage711()回复于 2005-10-09 11:34:57 得分 0

okTop

3 楼keiy()回复于 2005-10-09 12:14:36 得分 0

我还没写完你就结帖了,^_^.以下是全部两个函数的程序:  
   
  import   java.net.*;  
   
  public   class   test   {  
    public   static   int   str2Ip(String   ip)   throws   UnknownHostException   {    
                  InetAddress   address   =   InetAddress.getByName(ip);    
                  byte[]   bytes   =   address.getAddress();    
                  int   a,   b,   c,   d;    
                  a   =   byte2int(bytes[0]);    
                  b   =   byte2int(bytes[1]);    
                  c   =   byte2int(bytes[2]);    
                  d   =   byte2int(bytes[3]);    
                  int   result   =   (a   <<   24)   |   (b   <<   16)   |   (c   <<   8)   |   d;    
                  return   result;    
          }    
      public   static   int   byte2int(byte   b)   {    
                  int   l   =   b   &   0x07f;    
                  if   (b   <   0)   {    
                          l   |=   0x80;    
                  }    
                  return   l;    
          }    
   
   
  public   static   long   ip2long(String   ip)   throws   UnknownHostException   {    
                  int   ipNum   =   str2Ip(ip);    
                  return   int2long(ipNum);    
          }    
   
    public   static   long   int2long(int   i)   {    
                  long   l   =   i   &   0x7fffffffL;    
                  if   (i   <   0)   {    
                          l   |=   0x080000000L;    
                  }    
                  return   l;    
          }    
    public   static   String   long2ip(long   ip)   {  
    int   []   b=new   int[4]   ;  
    b[0]   =   (int)((ip   >>   24)   &   0xff);  
    b[1]   =   (int)((ip   >>   16)   &   0xff);  
    b[2]   =   (int)((ip   >>   8)   &   0xff);  
    b[3]   =   (int)(ip   &   0xff);  
    String   x;  
          Integer   p;  
          p=new   Integer(0);  
          x=p.toString(b[0])+"."+p.toString(b[1])+"."+p.toString(b[2])+"."+p.toString(b[3]);    
     
    return   x;    
     
    }  
   
  public   static   void   main(String[]   args)   throws   Exception   {  
  long   ip=ip2long("192.168.0.111");  
  System.out.println(ip);  
  System.out.println(long2ip(ip));  
   
  }  
   
  }  
  Top

相关问题

  • PHP中的Pack()函数,Java有哪个函数与之对应???
  • PHP到底有没有显示客户端IP的函数。
  • PHP的copy函数
  • ▲ JAVA函数大全!!! ▲
  • 关于java函数?
  • PHP 函数 转成 ASP函数
  • PHP函数fopen的问题
  • php处理日期函数!
  • 谁有JAVA的函数表
  • java中有popen()函数吗?

关键词

  • 函数
  • java
  • ip
  • php
  • 大侠
  • inetaddress
  • long
  • java中如何实现
  • bytes
  • address

得分解答快速导航

  • 帖主:savage711
  • keiy

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
x 提问