Why is long2ip conversion important?

Frequently Asked Questions about Convert long to IP address https://long2ip.com/faq/

Where is long2ip used?

Long2ip conversion is mainly used to swiftly look up an IP address without having to make time-consuming calculations to get there. IP addresses are often saved as long in databases because the server can perform queries more quickly, but this also means that they basically become almost unreadable for the human eye.

Why is long2ip conversion important?

IP addresses are often saved in databases as decimals, because it's easier for a database server to compare numbers with each other than to compare IP addresses. Because it's very difficult for people to be able to see or even imagine a long IP, we use long2ip converter tools to quickly look up an IP address.

What is long2ip conversion?

A long integer is a way of saving an IP address as a number in a database. A number is easier to use than an IP address in databases when making comparisons.

An IP address consists of 4 numbers that are divided by points, for example 8.8.8.8.

An IP address is "base 256", and to convert the IP address 8.8.8.8.to decimal (base 10) we use the following formula: 
8 x (256)^3 + 8 x (256)^2 + 8 x (256)^1 + 8 (256)^0

package com.test.long2ip;

public class Long2ipConversion {
public Long getPow(int i) {
return (long) Math.pow(256, i);
} public String long2ip(long long_) {
long i = long_;
short part3 = (short) (long_ % getPow(1));
i -= part3;
short part2 = (short) (i % getPow(2) / getPow(1));
i -= part2 * getPow(1);
short part1 = (short) (i % getPow(3) / getPow(2));
i -= part1 * getPow(2);
short part0 = (short) (i / getPow(3));
return part0 + "." + part1 + "." + part2 + "." + part3;
} public String long2ip(String long_) {
long intPointIp = Long.parseLong(long_.trim());
return long2ip(intPointIp);
} public long ip2long(String ip_) {
String[] ip_s = ip_.trim().split("\\.");
long res = 0;
short ii = 3;
for (String i : ip_s) {
res += Long.parseLong(i) * getPow(ii);
ii -= 1;
}
return res;
} } https://github.com/toywei/long2ip
 
上一篇:mongodb-$type、limit、skip、sort方法、索引、聚合


下一篇:20155203 2016-2017-2 《Java程序设计》第7周学习总结