当前位置:网站首页>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();
}
}
边栏推荐
- What parameters need to be reconfigured to replace the new radar of ROS robot
- 3D detection: fast visualization of 3D box and point cloud
- 566. Reshaping the matrix
- THINKPHP框架的优秀开源系统推荐
- 实现IP地址归属地显示功能、号码归属地查询
- Redis 核心数据结构 & Redis 6 新特性详
- Selenium库
- 搜索框效果的实现【每日一题】
- PostgreSQL array type, each splice
- "Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
猜你喜欢

Leetcode simple question sharing (20)

Thread pool reject policy best practices

Build a secure and trusted computing platform based on Kunpeng's native security

高等数学---第八章多元函数微分学1

Help tenants

Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1

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

2022-7-6 Leetcode 977. Square of ordered array

118. 杨辉三角
![SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
随机推荐
Help tenants
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
FCOS3D label assignment
Wired network IP address of VMware shared host
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
Xshell connection server changes key login to password login
作战图鉴:12大场景详述容器安全建设要求
THINKPHP框架的优秀开源系统推荐
Regular expression integer positive integer some basic expressions
属性关键字Aliases,Calculated,Cardinality,ClientName
Parameter keywords final, flags, internal, mapping keywords internal
PC端页面如何调用QQ进行在线聊天?
为租客提供帮助
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
call undefined function openssl_cipher_iv_length
Redis 核心数据结构 & Redis 6 新特性详
手里的闲钱是炒股票还是买理财产品好?
What parameters need to be reconfigured to replace the new radar of ROS robot
The difference between memory overflow and memory leak