DhcpInfo.ipAddress整数溢出?

时间:2015-03-26 03:44:33

标签: android ip overflow

DhcpInfo.ipAddress如何将IP地址存储到整数?

我连接到IP 192.168.1.101的本地网络,获得号码1 694 607 552.当将其转换为IP地址时,获得101.1.168.192(八位字节的反向顺序)。

我需要的是整数,表示正确的,而不是反向的IP ..所以3 232 235 877(代表" 192.168.1.101")在这种情况下,但我我失去了转换。我怎样才能做到这一点?我需要始终纠正代表IP的长整数,但我不确定,如果我的IP地址是例如,它会是什么样子192.168.1.253 ..

感谢您的帮助。

//编辑

public static String int_to_ip (int ip_address) {

    int octet1 = (ip_address & 0xFF000000) >>> 24;
    int octet2 = (ip_address & 0xFF0000) >>> 16;
    int octet3 = (ip_address & 0xFF00) >>> 8;
    int octet4 = ip_address & 0xFF;

    return new StringBuffer().append(octet1).append('.').append(octet2)
            .append('.').append(octet3).append('.')
            .append(octet4).toString();

}

0 个答案:

没有答案
相关问题