将点分式的IP地址转换成长整型

本文涉及的产品
公网NAT网关,每月750个小时 15CU
简介:  <pre class="java" name="code">/** * */ package test; import java.net.InetAddress; import java.net.UnknownHostException; import java.nio.ByteBuffer; /** * @author wKF46214 * *
 
/**
 * 
 */
package test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

/**
 * @author wKF46214
 * 
 */
public class IpConvert
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        try
        {
            long ip1 = convertIpFromString2Long("1.1.1.1");
            long ip2 = convertIpFromString2Long("255.255.255.255");
            System.out.println(ip1);
            System.out.println(ip2);
            long a = 3294967295L;
            String ip3 = convertIpFromLong2String(a);
            System.out.println(ip3);
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 将点分式的IP地址转换成长整型
     * @param ip
     * @return
     * @throws UnknownHostException
     */
    public static long convertIpFromString2Long(String ip) throws UnknownHostException
    {
        if (ip == null || ip.equals(""))
        {
            return 0;
        }

        InetAddress ipaddr = InetAddress.getByName(ip);
        byte[] bas = ipaddr.getAddress();
        int result = 0;
        result |= ((short) (bas[0] & 0x00ff) << 24);
        result |= ((short) (bas[1] & 0x00ff) << 16);
        result |= ((short) (bas[2] & 0x00ff) << 8);
        result |= (short) (bas[3] & 0x00ff);

        return convertUnsignedInt2Long(result);
    }

    public static long convertUnsignedInt2Long(int value)
    {
        long ret;

        ret = value >>> 1;
        ret <<= 1;
        ret |= (value << 31) >>> 31;

        return ret;
    }

    /**
     * 将长整型的IP地址转换成点分式
     * 
     * @param ip
     * @return
     * @throws UnknownHostException
     */
    public static String convertIpFromLong2String(long ip) throws UnknownHostException
    {
        if (ip <= 0)
        {
            return "";
        }

        ByteBuffer buf = ByteBuffer.allocate(4);
        // buf.order(ByteOrder.LITTLE_ENDIAN);//必须转换成小头序,才能正确显示IP
        buf.putInt(convertLong2UnsignedInt(ip));

        InetAddress addr = InetAddress.getByAddress(buf.array());
        return addr.getHostAddress();
    }

    public static int convertLong2UnsignedInt(long value)
    {
        int ret;

        ret = (int) (value & 0x00000000ffffffff);

        return ret;
    }

}

 


 

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
基于阿里云,构建一个企业web应用上云经典架构,让IT从业者体验企业级架构的实战训练。
目录
相关文章
|
4小时前
socket字节序转换与地址转换函数记录
【代码】socket字节序转换与地址转换函数记录。
18 0
|
5月前
IP地址转换函数
IP地址转换函数
21 0
|
12月前
一日一技:负长整数如何转换为IPv4地址?
一日一技:负长整数如何转换为IPv4地址?
63 2
|
算法
IP地址转换整型(算法练习)
IP地址转换整型(算法练习)
101 0
IP地址转换整型(算法练习)
|
Java
java 类型转化:(数值型~整型、浮点型←----→非数值型~字符串、字节;以及 字符串←----→字节(字节数组)
java 类型转化:(数值型~整型、浮点型←----→非数值型~字符串、字节;以及 字符串←----→字节(字节数组)
143 0
|
存储
G - IP地址转换
IP地址总是由4个0-255的数字以"."隔开的形式来显示给用户,例如192.168.0.1。在计算机中,一个IP地址用4字节来依次存储其从右到左的4个数字部分,每个字节(8比特)以2进制的形式存储相应的IP地址数字,请你实现一个从IP地址的显示格式到计算机存储格式的转换。
906 0