当前位置:网站首页>IP tools
IP tools
2022-07-07 19:39:00 【Scarecrow 0.0】
public class IPUtils {
public IPUtils() {
}
public static String convertIPToString(long ipLong) {
if (ipLong < 0L) {
ipLong += 4294967296L;
}
StringBuilder builder = new StringBuilder(15);
builder.append(ipLong >> 24);
builder.append('.');
builder.append((ipLong & 16777215L) >> 16);
builder.append('.');
builder.append((ipLong & 65535L) >> 8);
builder.append('.');
builder.append(ipLong & 255L);
return builder.toString();
}
public static Long convertIPToLong(String ipAddr, Long defaultValue) {
try {
return convertIPToLong(ipAddr);
} catch (IllegalArgumentException var3) {
return defaultValue;
}
}
public static long convertIPToLong(String ipAddr) throws IllegalArgumentException {
if (ipAddr != null && isIPAddr(ipAddr)) {
String[] ips = ipAddr.split("\\.");
long ipLong = 0L;
ipLong += Long.parseLong(ips[0]) << 24;
ipLong += Long.parseLong(ips[1]) << 16;
ipLong += Long.parseLong(ips[2]) << 8;
ipLong += Long.parseLong(ips[3]);
return ipLong;
} else {
throw new IllegalArgumentException("`" + ipAddr + "` is not a valid IP address!");
}
}
public static Integer convertIPToInt(String ipAddr, Integer defaultValue) {
try {
return convertIPToInt(ipAddr);
} catch (IllegalArgumentException var3) {
return defaultValue;
}
}
public static int convertIPToInt(String ipAddr) {
long ipVal = convertIPToLong(ipAddr);
if (ipVal > 2147483647L) {
ipVal -= 4294967296L;
}
return Long.valueOf(ipVal).intValue();
}
public static boolean isIPAddr(String addr) {
if (addr != null && addr.trim().length() != 0) {
String[] ips = addr.split("\\.");
if (ips.length != 4) {
return false;
} else {
try {
int ipa = Integer.parseInt(ips[0]);
int ipb = Integer.parseInt(ips[1]);
int ipc = Integer.parseInt(ips[2]);
int ipd = Integer.parseInt(ips[3]);
return ipa >= 0 && ipa <= 255 && ipb >= 0 && ipb <= 255 && ipc >= 0 && ipc <= 255 && ipd >= 0 && ipd <= 255;
} catch (NumberFormatException var6) {
return false;
}
}
} else {
return false;
}
}
}
边栏推荐
- Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
- State mode - Unity (finite state machine)
- [Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
- 2022.07.05
- Where does brain hole come from? New research from the University of California: creative people's neural connections will "take shortcuts"
- 小试牛刀之NunJucks模板引擎
- Kirin Xin'an with heterogeneous integration cloud financial information and innovation solutions appeared at the 15th Hunan Financial Technology Exchange Conference
- Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
- Numpy——2.数组的形状
- Uvalive – 4621 CAV greed + analysis "suggestions collection"
猜你喜欢

The top of slashdata developer tool is up to you!!!

网易云信参与中国信通院《实时音视频服务(RTC)基础能力要求及评估方法》标准编制...

2022.07.05

Install mysql8 for Linux X ultra detailed graphic tutorial

Numpy——2.数组的形状

In the first half of 2022, I found 10 books that have been passed around by my circle of friends

2022.07.02

小试牛刀之NunJucks模板引擎

PMP practice once a day | don't get lost in the exam -7.7

Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
随机推荐
杰理之相同声道的耳机不允许配对【篇】
鸿蒙智能家居【1.0】
LeetCode 648(C#)
编译原理 实验一:词法分析器的自动实现(Lex词法分析)
超分辨率技术在实时音视频领域的研究与实践
Version 2.0 of tapdata, the open source live data platform, has been released
Tips and tricks of image segmentation summarized from 39 Kabul competitions
杰理之开机自动配对【篇】
What does "true" mean
AI writes a poem
Seize Jay Chou
2022.07.04
LeetCode 515(C#)
现在股票开户可以直接在网上开吗?安全吗。
Numpy——axis
Draw squares with Obama (Lua)
IP 工具类
Responsibility chain model - unity
LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
CMD command enters MySQL times service name or command error (fool teaching)