IP地址 与 十进制 IP Number 互转(IPv4IPv6)

    科技2026-03-17  8

    IPv4 到 IP Number 转换公式与代码

    IP Number = 16777216*w + 65536*x + 256*y + z (1) where IP Address = w.x.y.z For example, if the IP address is "202.186.13.4", then its IP Number will be "3401190660", based on the formula (1). IP Address = 202.186.13.4 So, w = 202, x = 186, y = 13 and z = 4 IP Number = 16777216*202 + 65536*186 + 256*13 + 4 = 3388997632 + 12189696 + 3328 + 4 = 3401190660

    PHP 代码

    function Dot2LongIP ($IPaddr) { if ($IPaddr == "") { return 0; } else { $ips = explode(".", "$IPaddr"); return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256); } }

    Java 代码

    long Dot2LongIP(String ipstring) { String[] ipAddressInArray = ipstring.split("\\."); long result = 0; long ip = 0; for (int x = 3; x >= 0; x--) { ip = Long.parseLong(ipAddressInArray[3 - x]); result |= ip << (x << 3); } return result; }

    MySQL 代码

    CREATE FUNCTION Dot2LongIP (ip text) RETURNS bigint BEGIN DECLARE ipnum bigint; SET ipnum = (SELECT INET_ATON(ip)); RETURN ipnum; END

    IP Number 到 IPv4 转换公式

    IP Address = w.x.y.z To reverse IP number to IP address, w = int ( IP Number / 16777216 ) % 256 x = int ( IP Number / 65536 ) % 256 y = int ( IP Number / 256 ) % 256 z = int ( IP Number ) % 256 where % is the modulus operator and int returns the integer part of the division.

    PHP 代码

    function Long2DotIP ($IPNum) { if ($IPNum == "") { return "0.0.0.0"; } else { return (($IPNum / 16777216) % 256) . "." . (($IPNum / 65536) % 256) . "." . (($IPNum / 256) % 256) . "." . ($IPNum % 256); } }

    Java 代码

    String Long2DotIP(long ipnum) { String result = ""; result = ((ipnum / 16777216) % 256) + "." + ((ipnum / 65536) % 256) + "." + ((ipnum / 256) % 256) + "." + (ipnum % 256); return result; }

    MySQL代码

    CREATE FUNCTION Long2DotIP (ipnum bigint) RETURNS text BEGIN DECLARE ip text; SET ip = (SELECT INET_NTOA(ipnum)); RETURN ip; END

     

    IPv6 到 IP Number 转换公式

    IP Number = (65536^7)*a + (65536^6)*b + (65536^5)*c + (65536^4)*d + (65536^3)*e + (65536^2)*f + 65536*g + h (1) where IP Address = a:b:c:d:e:f:g:h For example, if the IP address is "2001:0db8:0000:0042:0000:8a2e:0370:7334", then its IP Number will be "42540766411282594074389245746715063092", based on the formula (1). IP Address (in hexadecimal) = 2001:0db8:0000:0042:0000:8a2e:0370:7334 IP Address (in decimal) = 8193:3512:0:66:0:35374:880:29492 IP Number = (65536^7)*8193 + (65536^6)*3512 + (65536^5)*0 + (65536^4)*66 + (65536^3)*0 + (65536^2)*35374 + 65536*880 + 29492 = 5192296858534827628530496329220096*8193 + 79228162514264337593543950336*3512 + 1208925819614629174706176*0 + 18446744073709551616*66 + 281474976710656*0 + 4294967296*35374 + 57671680 + 29492 = 42540488161975842760550356425300246528 + 278249306750096353628526353580032 + 0 + 1217485108864830406656 + 0 + 151930173128704 + 57671680 + 29492 = 42540766411282594074389245746715063092

    PHP 代码

    function Dot2LongIP($ipv6) { return (string) gmp_import(inet_pton($ipv6)); }

    Java 代码

    java.math.BigInteger Dot2LongIP(String ipv6) { java.net.InetAddress ia = java.net.InetAddress.getByName(ipv6); byte byteArr[] = ia.getAddress(); if (ia instanceof java.net.Inet6Address) { java.math.BigInteger ipnumber = new java.math.BigInteger(1, byteArr); return ipnumber; } }

     

    IP Number 到 IPv6 转换公式

    IP Address = a:b:c:d:e:f:g:h To reverse IP number to IP address, a = int ( IP Number / (65536^7) ) % 65536 b = int ( IP Number / (65536^6) ) % 65536 c = int ( IP Number / (65536^5) ) % 65536 d = int ( IP Number / (65536^4) ) % 65536 e = int ( IP Number / (65536^3) ) % 65536 f = int ( IP Number / (65536^2) ) % 65536 g = int ( IP Number / 65536 ) % 65536 h = IP Number % 65536 where % is the modulus operator and int returns the integer part of the division. NOTE: All parts need to be converted into hexadecimal to be part of the IPv6 address.

    PHP 代码

    function Long2DotIP($integer) { return inet_ntop(str_pad(gmp_export($integer), 16, "\0", STR_PAD_LEFT)); }

    Java 代码

    String Long2DotIP(String integer) { String ipstr = new java.math.BigInteger(integer).toString(16); String padding = new String(new char[32 - ipstr.length()]).replace("\0", "0"); String retval = padding + ipstr; retval = retval.replaceAll("(.{4})", "$1:").substring(0, 39); return retval; }

     

    内容整理自:https://lite.ip2location.com/faqs

     

    Processed: 0.013, SQL: 9