【问题标题】:Java ip2long equivalentJava ip2long 等价物
【发布时间】:2012-06-03 18:25:42
【问题描述】:

您好,我有一个数据库可以从中选择 IP 位置>

脚本在 php 中,我正在将其转换为 java,但我不知道 java 中 ip2long('127.0.0.1' )); 的等价物是什么

【问题讨论】:

标签: java php


【解决方案1】:
public static Long ipToLong(String stringIp) {
        return Arrays.stream(stringIp.split("\\."))
                .mapToLong(Long::parseLong)
                .reduce(0,
                        (a, b) -> (a << 8) + b);
}

public static String ipToString(Long longIp){
        return ((longIp >> 24) & 0xFF) + "." +
                ((longIp >> 16) & 0xFF) + "." +
                ((longIp >> 8) & 0xFF) + "." +
                (longIp & 0xFF);
}

【讨论】:

    【解决方案2】:

    我希望下面的链接对你有所帮助,反之亦然

    https://www.mkyong.com/java/java-convert-ip-address-to-decimal-number.

    以上链接提供的方法:

    public static long ipToLong(String ipAddress) {
        String[] ipAddressInArray = ipAddress.split("\\.");
    
        long result = 0;
        for (int i = 0; i < ipAddressInArray.length; i++) {     
            int power = 3 - i;
            int ip = Integer.parseInt(ipAddressInArray[i]);
            result += ip * Math.pow(256, power);
        }
    
        return result;
    }
    
    public static String longToIp(long ip) {
        StringBuilder result = new StringBuilder(15);
    
        for (int i = 0; i < 4; i++) {
            result.insert(0,Long.toString(ip & 0xff));
            if (i < 3) {
                result.insert(0,'.');
            }
            ip = ip >> 8;
        }
    
        return result.toString();
    }
    

    【讨论】:

      【解决方案3】:

      这个适用于 IPv4 和 IPv6:

      public static String toLongString(final String ip) {
          try {
              final InetAddress address = InetAddress.getByName(ip);
              final byte[] bytes = address.getAddress();
              final byte[] uBytes = new byte[bytes.length + 1]; // add one byte at the top to make it unsigned
              System.arraycopy(bytes, 0, uBytes, 1, bytes.length);
              return new BigInteger(uBytes).toString();
          } catch (final UnknownHostException e) {
              throw new IllegalArgumentException(e);
          }
      }
      

      【讨论】:

        【解决方案4】:

        我首先将字符串转换为八位字节:

        static final String DEC_IPV4_PATTERN = "^(([0-1]?\\d{1,2}\\.)|(2[0-4]\\d\\.)|(25[0-5]\\.)){3}(([0-1]?\\d{1,2})|(2[0-4]\\d)|(25[0-5]))$";
        
        static byte[] toOctets(String address){
        
            if(address==null){
                throw new NullPointerException("The IPv4 address cannot be null.");
            }
        
            if(!address.matches(DEC_IPV4_PATTERN)){
                throw new IllegalArgumentException(String.format("The IPv4 address is invalid:%s ",address));
            }
        
            //separate octets into individual strings
            String[] numbers = address.split("\\.");
        
            //convert octets to bytes.
            byte[] octets = new byte[4];
            for(int i = 0; i < octets.length; i++){
                octets[i] = Integer.valueOf(numbers[i]).byteValue();
            }
            return octets;
        }
        

        然后将八位字节转换为 BigInteger,因为它接受一个字节数组,然后从它转换为一个整数:

        static int toInteger(byte[] octets){
            if(octets==null){
                throw new NullPointerException("The array of octets cannot be null");
            }
        
            if(octets.length != 4){
                throw new IllegalArgumentException(String.format("The byte array must contain 4 octets: %d",octets.length));
            }
        
            return new BigInteger(octets).intValue();
        }
        

        从这里,你可以简单地做:

        String address = "127.0.0.1";
        System.out.println(toInteger(toOctets(address)));
        

        或者创建一个名为ip2long(String address )的函数

        【讨论】:

          【解决方案5】:

          我认为在 Java 中没有标准的 API 可以做到这一点,但是

          1/ InetAddress 类为您提供了一种获取字节数组的方法。

          2/如果你真的需要一个整数,你可以使用这个sn-p,在http://www.myteneo.net/blog/-/blogs/java-ip-address-to-integer-and-back/找到

          public static String intToIp(int i) {
              return ((i >> 24 ) & 0xFF) + "." +
          
                     ((i >> 16 ) & 0xFF) + "." +
          
                     ((i >>  8 ) & 0xFF) + "." +
          
                     ( i        & 0xFF);
          
          }
          
          public static Long ipToInt(String addr) {
              String[] addrArray = addr.split("\\.");
          
              long num = 0;
          
              for (int i=0;i<addrArray.length;i++) {
          
                  int power = 3-i;
          
                  num += ((Integer.parseInt(addrArray[i])%256 * Math.pow(256,power)));
          
              }
          
              return num;
          
          }
          

          【讨论】:

          • 不要尝试“太”实际上看起来我们有相同的来源,除了我引用了我的事实;=)
          【解决方案6】:

          基本上,这会将您的带点 IP 地址字符串转换为长字符串。

          public static Long Dot2LongIP(String dottedIP) {
              String[] addrArray = dottedIP.split("\\.");        
              long num = 0;        
              for (int i=0;i<addrArray.length;i++) {            
                  int power = 3-i;            
                  num += ((Integer.parseInt(addrArray[i]) % 256) * Math.pow(256,power));        
              }        
              return num;    
          }
          

          【讨论】:

          • 非常感谢将不得不尝试它以确保它得到我的确切结果。你节省了我的时间:)
          猜你喜欢
          • 2013-01-15
          • 1970-01-01
          • 2016-10-10
          • 2010-11-13
          • 2019-11-03
          • 2010-11-19
          • 2011-01-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多