当前位置:网站首页>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();
}
}
边栏推荐
- Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
- Cargo placement problem
- Realization of search box effect [daily question]
- Oracle advanced (V) schema solution
- Environment configuration of lavarel env
- Drawerlayout suppress sideslip display
- [high frequency interview questions] difficulty 2.5/5, simple combination of DFS trie template level application questions
- 請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
- 华为镜像地址
- 118. 杨辉三角
猜你喜欢
C语言数组相关问题深度理解
Flask session forged hctf admin
Redis can only cache? Too out!
2022-7-7 Leetcode 844. Compare strings with backspace
交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
作战图鉴:12大场景详述容器安全建设要求
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
室内ROS机器人导航调试记录(膨胀半径的选取经验)
2022-7-6 beginner redis (I) download, install and run redis under Linux
Did login metamask
随机推荐
Laravel5 call to undefined function OpenSSL cipher IV length() error php7 failed to open OpenSSL extension
3D Detection: 3D Box和点云 快速可视化
供应链供需预估-[时间序列]
现在网上开户安全么?那么网上开户选哪个证券公司?
Interface automation test - solution of data dependency between interfaces
Move base parameter analysis and experience summary
[daily training] 648 Word replacement
2022-7-7 Leetcode 844. Compare strings with backspace
Dry goods | summarize the linkage use of those vulnerability tools
Getting started with MySQL
参数关键字Final,Flags,Internal,映射关键字Internal
Learning breakout 2 - about effective learning methods
请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
为租客提供帮助
move base参数解析及经验总结
Is the spare money in your hand better to fry stocks or buy financial products?
Is it safe to open an account online now? Which securities company should I choose to open an account online?
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
Custom thread pool rejection policy
Mysql怎样控制replace替换的次数?