当前位置:网站首页>IP and long integer interchange
IP and long integer interchange
2022-07-07 14:04:00 【It is small new】
IP Swap with long integer
*
*
*/
public class IPUtil {
public static void main(String[]args){
String ip="126.56.78.59";
long ipLong=ipToLong(ip);
System.out.println(ipLong);
System.out.println(longToIP(ipLong));
}
/**
* take 127.0.0.1 Formal IP The address is converted to a decimal integer There is no error handling here
* Move left operation (<<) Weight each number The power of the first paragraph is 2 Of 24 Power The power of the second paragraph is 2 Of 16 Power
* The power of the third paragraph is 2 Of 8 Power The last paragraph is 1
*/
public static long ipToLong(String ipadress){
long[]ip=new long[4];
// First find IP In the address string . The location of
int position1=ipaddress.indexOf(".");
int position2=ipaddress.indexOf(".",position1+1);
int position3=ipaddress.indexOf(".",position2+1);
// Each one . Convert the string between to integer
ip[0]=Long.parseLong(ipadress, substring(0,position1));
ip[1] = Long.parseLong(ipaddress.substring(position1 + 1, position2));
ip[2] = Long.parseLong(ipaddress.substring(position2 + 1, position3));
ip[1] = Long.parseLong(ipaddress.substring(position3 + 1));
return (ip[0]<<24)+(ip[1]<<16)+(ip[2]<<8)+ip[3];
}
/**
* Convert decimal integer form to 127.0.0.1 Formal IP Address Move the integer value to the right (>>) Move right 24 position , When moving to the right, the high position makes up 0, The number obtained is the first paragraph IP.
* adopt & The operator Set the high order of the integer value to 0, Move right again 16 position The number obtained is the second paragraph ip
* The high of the integer value through the and operator 16 Set as 0, Move right again 8 position , The number obtained is the third paragraph IP.
The high of the integer value through the and operator 24 Set as 0, The figure obtained is the fourth paragraph IP.
*/
public static String longToLong(long ipaddress){
StringBuffer sb=new StringBuffer("");
// Move right directly 24 position
sb.append(String.valueOf((ipaddress>>>24)));
sb.append(".");
// Will be high 8 Location 0 And then move right 16 position
sb.append(String.valueOf((ipaddress&0x00FFFFFF)>>>16));
sb.append(".");
// Will be high 16 Location 0 And then move right 8 position
sb.append(String.valueOf((ipaddress&0x0000FFFF)>>>8));
sb.append(".");
// Will be high 24 Location 0
sb.append(String.valueOf((ipaddress & 0x000000FF)));
return sb.toString();
}
}
边栏推荐
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
- DID登陆-MetaMask
- [untitled]
- 现在网上开户安全么?那么网上开户选哪个证券公司?
- Supply chain supply and demand estimation - [time series]
- 高等数学---第八章多元函数微分学1
- Xshell connection server changes key login to password login
- FCOS3D label assignment
- AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
- Lavarel之环境配置 .env
猜你喜欢
2022-7-7 Leetcode 844. Compare strings with backspace
高等數學---第八章多元函數微分學1
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
MySQL error 28 and solution
Wired network IP address of VMware shared host
Leetcode simple question sharing (20)
118. Yanghui triangle
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
Deep understanding of array related problems in C language
随机推荐
请问指南针股票软件可靠吗?交易股票安全吗?
供应链供需预估-[时间序列]
AI talent cultivation new ideas, this live broadcast has what you care about
请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
现在网上开户安全么?那么网上开户选哪个证券公司?
【日常训练】648. 单词替换
2022-7-7 Leetcode 844.比较含退格的字符串
Parameter keywords final, flags, internal, mapping keywords internal
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
Redis can only cache? Too out!
内存溢出和内存泄漏的区别
Build a secure and trusted computing platform based on Kunpeng's native security
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
Navicat run SQL file import data incomplete or import failed
【网络安全】sql注入语法汇总
属性关键字Aliases,Calculated,Cardinality,ClientName
Battle Atlas: 12 scenarios detailing the requirements for container safety construction
ES日志报错赏析-Limit of total fields
[daily training] 648 Word replacement