当前位置:网站首页>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();
}
}
边栏推荐
- [fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
- Xshell connection server changes key login to password login
- THINKPHP框架的优秀开源系统推荐
- mysql ”Invalid use of null value“ 解决方法
- How does MySQL control the number of replace?
- SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
- Mysql怎样控制replace替换的次数?
- Leetcode simple question sharing (20)
- [1] Basic knowledge of ros2 - summary version of operation commands
- Excuse me, when using Flink SQL sink data to Kafka, the execution is successful, but there is no number in Kafka
猜你喜欢

Help tenants

AI人才培育新思路,这场直播有你关心的

Redis只能做缓存?太out了!

Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance

室内ROS机器人导航调试记录(膨胀半径的选取经验)

Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)

Vmware 与主机之间传输文件

作战图鉴:12大场景详述容器安全建设要求

Build a secure and trusted computing platform based on Kunpeng's native security
![SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
随机推荐
2022-7-6 Leetcode 977. Square of ordered array
Cesium 已知一点经纬度和距离求另一个点的经纬度
Is the compass stock software reliable? Is it safe to trade stocks?
云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
2022-7-6 Leetcode 977.有序数组的平方
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
PHP中用下划线开头的变量含义
566. Reshaping the matrix
mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因
648. Word replacement: the classic application of dictionary tree
Environment configuration of lavarel env
Getting started with MySQL
【日常训练--腾讯精选50】231. 2 的幂
Mysql怎样控制replace替换的次数?
Use day JS let time (displayed as minutes, hours, days, months, and so on)
Vmware共享主机的有线网络IP地址
toRaw和markRaw