public long getDecimalfromIP(String addr)
  {
     StringTokenizer st = new StringTokenizer(addr,".");
     long n1 = Integer.parseInt(st.nextToken());
     long n2 = Integer.parseInt(st.nextToken());
     long n3 = Integer.parseInt(st.nextToken());
     long n4 = Integer.parseInt(st.nextToken());
     long ret = 0xFFFFFFFF & ((n1 << 24) | (n2 << 16) | (n3 << 8) | n4);
     return((long)ret);
  }  public String getIPfromDecimal(long ip)
  {
     String s1 = new String(new Long((long)(ip >> 24) & 0xFF).toString());
     String s2 = new String(new Long((long)(ip >> 16) & 0xFF).toString());
     String s3 = new String(new Long((long)(ip >> 8) & 0xFF).toString());
     String s4 = new String(new Long((long)ip & 0xFF).toString());
     return(new String(s1 + "." + s2 + "." + s3 + "." + s4));
  }

解决方案 »

  1.   

    我想要的是我已经知道这个人的ip(string类型),但是我要用这个ip来发送信息的时候却要用getbyname()来取ip做为套接字里面的ip,我觉得浪费了时间和资源,有没有string-》ip的转换呢
      

  2.   

    有没有string-》ip的转换呢 ——没有,古往今来都没有那种好事儿!!!!
    像skyyoung(路人甲)兄那样老老实实自己写吧!!!
      

  3.   

    public Socket(String host,
                  int port)
           throws UnknownHostException,
                  IOException
      

  4.   

    byte[] bb = InetAddress.getByName("1.2.3.4").getAddress();
    for(int i = 0; i < bb.length; i++)
       System.out.println(bb[i]);
      

  5.   

    你的想法很好。
    可是没有。
    有关ip的方法都在InetAddress类中了。你可以查一查。 
    不过,为了不浪费时间的话,你还是用InetAddress.getByName(String address)的好。