Many convert string ip to long java code demo like this
private static int str2Ip(String ip) {
String[] ss = ip.split("\\.");
int a, b, c, d;
a = Integer.parseInt(ss[0]);
b = Integer.parseInt(ss[1]);
c = Integer.parseInt(ss[2]);
d = Integer.parseInt(ss[3]);
return (a << 24) | (b << 16) | (c << 8) | d;
}
private static long ip2long(String ip) {
return int2long(str2Ip(ip));
}
private static long int2long(int i) {
long l = i & 0x7fffffffL;
if (i < 0) {
l |= 0x080000000L;
}
return l;
}
why we don't use other Java method instead method int2long
? e.g.
Integer a = 0;
Long.valueof(a.toString());