当前位置:网站首页>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();
}
}
边栏推荐
- FCOS3D label assignment
- Flask session forged hctf admin
- Battle Atlas: 12 scenarios detailing the requirements for container safety construction
- TPG x AIDU | AI leading talent recruitment plan in progress!
- call undefined function openssl_ cipher_ iv_ length
- AI talent cultivation new ideas, this live broadcast has what you care about
- 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
- Transferring files between VMware and host
- 高等數學---第八章多元函數微分學1
- Details of redis core data structure & new features of redis 6
猜你喜欢

2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
![Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]](/img/35/5224252624cc76f42cbd0fd5c81d8c.png)
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]

Did login metamask

Selenium库

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

118. Yanghui triangle

Redis can only cache? Too out!

2022-7-6 Leetcode 977.有序数组的平方

为租客提供帮助

docker部署oracle
随机推荐
How to check the ram and ROM usage of MCU through Keil
MySQL error 28 and solution
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
Details of redis core data structure & new features of redis 6
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因
Use day JS let time (displayed as minutes, hours, days, months, and so on)
Realize the IP address home display function and number home query
566. Reshaping the matrix
搜索框效果的实现【每日一题】
PHP中用下划线开头的变量含义
C语言数组相关问题深度理解
Laravel form builder uses
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
Data refresh of recyclerview
The difference between memory overflow and memory leak
[untitled]
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
Navicat运行sql文件导入数据不全或导入失败
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?