Lua ip to int 和 int to ip

1function _M.ipToInt( str ) 2 local num = 0 3 if str and type(str)=="string" then 4 local o1,o2,o3,o4 = str:match("(%d+)%.(%d+)%.(%d+)%.(%d+)" ) 5 num = 2^24*o1 + 2^16*o2 + 2^8*o3 + o4 6 end 7 return num 8end 9-- inter to ip 10function _M.intToIp( n ) 11 if n then 12 n = tonumber(n) 13 local n1 = math.floor(n / (2^24)) 14 local n2 = math.floor((n - n1*(2^24)) / (2^16)) 15 local n3 = math.floor((n - n1*(2^24) - n2*(2^16)) / (2^8)) 16 local n4 = math.floor((n - n1*(2^24) - n2*(2^16) - n3*(2^8))) 17 return n1.."."..n2.."."..n3.."."..n4 18 end 19 return "0.0.0.0" 20end
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java判断字符串是否为数字或中文或字母

1.判断字符串是否仅为数字:1用JAVA自带的函数public static boolean isNumeric(String str){  for (int i  str.length();i0;){      if (!Character.isDigit(str.charAt(i))){

java字符串比较和jdkequals源码分析

 package com.cnse.demo;/  比较两个字符串相等 /public class StringTest { public static void main(String args) {  int checkLength  0;  String str

IP地址的正则表达式

finalStringREGX_IP"((2505|204\\d|1\\d{2}|19\\d|\\d)\\.){3}(2505|204\\d|1\\d{2}|19\\d|\\d)";在过滤json数据中的ip,需要判断异常ip值,我的写法:^"{2}\b((2505|204

Cppcheck简单测评

测评代码如下:include <cstdioinclude <stringint main(void){ // 多了或者少了格式化参数 ::printf("%d%d%d\n", int(1), int(2)); ::printf("%d%d%d\n", int(1), int(2

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid