IP转唯一整数

先转二进制,左边补齐0,再右边补齐0,再转十进制计算

$ip = '0210.110.11.49';
function ipToInt($ip){
    $iparr=explode('.',$ip);
    $d=0;
    foreach ($iparr as $key=>$value){
        $t=decbin($value);
        $t=str_pad($t,8,'0',STR_PAD_LEFT);
        $t=str_pad($t,32-8*$key,'0',STR_PAD_RIGHT);
        $d+=bindec($t);

    }
    return $d;
}
echo  $ip.'<br />';
$ip_int = ipToInt($ip);
echo $ip_int.'<br />';
echo long2ip($ip_int);

 

 

$ip = '0210.110.11.49';

function ipToInt($ip){
    $iparr = explode('.',$ip);
    return (intval($iparr[0]<<24))|
(intval($iparr[1])<<16)|(intval($iparr[2])<<8)| 
(intval($iparr[3]));//|按位或运算
}

echo  $ip.'<br />';
$ip_int = ipToInt($ip);
echo $ip_int.'<br />';
echo long2ip($ip_int);

 

 

$ip = '0210.110.11.49';

function ipToInt($ip){
    $iparr = explode('.',$ip);
    $num = 0;
    for($i=0;$i<count($iparr);$i++){
        $num += intval($iparr[$i]) * pow(256,count($iparr)-($i+1));//pow() 函数返回 x 的 y 次方。
    }
    return $num;
}

echo  $ip.'<br />';
$ip_int = ipToInt($ip);
echo $ip_int.'<br />';
echo long2ip($ip_int);

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
leetcode 1185 leetcode-js JS 只解决 leetcode 问题。 简单的 数字 问题名称 完毕 1 二和 :check_mark_button: 9 回文数 :check_mark_button: 21 合并两个排序列表 :check_mark_button: 26 从排序数组中删除重复项 :check_mark_button: 27 删除元素 :check_mark_button: 28 实现 strStr :check_mark_button: 88 合并排序数组 :check_mark_button: 169 多数元素 :check_mark_button: 404 左叶总和 :check_mark_button: 412 嘶嘶声 :check_mark_button: 276 数字补码 :check_mark_button: 728 自除数 :check_mark_button: 771 珠宝和石头 :check_mark_button: 832 翻图像 :check_mark_button: 977 有序数组的平方 :check_mark_button: 1108 修改 IP 地址 :check_mark_button: 1185 一周中的天 :check_mark_button: 1221 在平衡字符串中拆分字符串 :check_mark_button: 1281 减去一个整数的乘积和数字之和 :check_mark_button: 1351 计算排序矩阵中的负数 :check_mark_button: 1365 有多少数字比当前数字小 :check_mark_button: 1417 重新格式化字符串 :check_mark_button: 1480 一维数组的运行总和 :check_mark_button: 1486 数组中的异或运算 :check_mark_button: 1748 独特元素的总和 :check_mark_button: 中等的 数字 问题名称 完毕 2 两个数字相加 :check_mark_button: 3 无重复字符的最长子串 :check_mark_button: 11 盛水最多的容器 :check_mark_button: 15 3 总和 :check_mark_button: 31 下一个排列 :check_mark_button: 34 在排序数组中查找元素的第一个和最

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值