当前位置:网站首页>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();
}
}
边栏推荐
- Vmware 与主机之间传输文件
- 最长上升子序列模型 AcWing 1014. 登山
- Use day JS let time (displayed as minutes, hours, days, months, and so on)
- 云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
- 内存溢出和内存泄漏的区别
- 《厌女:日本的女性嫌恶》摘录
- 参数关键字Final,Flags,Internal,映射关键字Internal
- Transferring files between VMware and host
- 请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数
- Drawerlayout suppress sideslip display
猜你喜欢

《厌女:日本的女性嫌恶》摘录

"New red flag Cup" desktop application creativity competition 2022

How to check the ram and ROM usage of MCU through Keil
![Supply chain supply and demand estimation - [time series]](/img/2c/82d118cfbcef4498998298dd3844b1.png)
Supply chain supply and demand estimation - [time series]

Use day JS let time (displayed as minutes, hours, days, months, and so on)

Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1

Vmware 与主机之间传输文件

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

2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out

2022-7-6 beginner redis (I) download, install and run redis under Linux
随机推荐
mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因
Lavarel之环境配置 .env
Mysql怎样控制replace替换的次数?
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
Getting started with MySQL
Help tenants
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
Beginner XML
Realization of search box effect [daily question]
手把手教会:XML建模
Supply chain supply and demand estimation - [time series]
Redis can only cache? Too out!
Parameter keywords final, flags, internal, mapping keywords internal
Move base parameter analysis and experience summary
【网络安全】sql注入语法汇总
Cesium 已知一点经纬度和距离求另一个点的经纬度
Toraw and markraw
What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?