Skip to content

Commit

Permalink
Merge 0fbb662 into 694b57d
Browse files Browse the repository at this point in the history
  • Loading branch information
iamazy committed Oct 26, 2020
2 parents 694b57d + 0fbb662 commit 8942cd2
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -985,5 +985,25 @@ public static boolean isValidIpV4Address(String value) {
return periods == 3;
}

public static long ip2Long(final String ip) {
String[] ipItem = ip.split("\\.");
assert ipItem.length == 4;
long[] ipNum = new long[4];
ipNum[0] = Long.parseLong(ipItem[0]);
ipNum[1] = Long.parseLong(ipItem[1]);
ipNum[2] = Long.parseLong(ipItem[2]);
ipNum[3] = Long.parseLong(ipItem[3]);
return (ipNum[0] << 24) + (ipNum[1] << 16) + (ipNum[2] << 8) + ipNum[3];
}

public static String long2Ip(final long num) {
return (num >>> 24) +
"." +
((num & 0x00FFFFFF) >>> 16) +
"." +
((num & 0x0000FFFF) >>> 8) +
"." +
(num & 0x000000FF);
}

}

0 comments on commit 8942cd2

Please sign in to comment.