您的值不正确 70.30.65.9 将与 1176387849 对应,但绝不会与 9766322441 对应。
IP转十进制的C++例子:
#include <iostream.h>
main()
{
// Initialize the variables
unsigned long a,b,c,d,base10IP;
// Get the IP address from user
cout << "\nEnter an IP address in dotted quad notation (x.x.x.x)";
cout << "\nwith each section seperated by a space: ";
cin >> a >> b >> c >> d;
// Do calculations to convert IP to base 10
a *= 16777216;
b *= 65536;
c *= 256;
base10IP = a + b + c + d;
// Output new IP address
cout << "\nThe converted address is: " << base10IP << '\n';
}
这是将 IP 转换为十进制的 JAVA 类:
-
/**
* @author Charles Johnson
* from http://www.technojeeves.com/joomla/index.php/free/58-convert-ip-address-to-number
*/
public class IpConverter {
public static void main(String[] args) {
System.out.println(longToIp(Long.valueOf(args[0], 16)));
}
public static String toHex(String ipAddress) {
return Long.toHexString(IpConverter.ipToLong(ipAddress));
}
public static long ipToLong(String ipAddress) {
long result = 0;
String[] atoms = ipAddress.split("\\.");
for (int i = 3; i >= 0; i--) {
result |= (Long.parseLong(atoms[3 - i]) << (i * 8));
}
return result & 0xFFFFFFFF;
}
public static String longToIp(long ip) {
StringBuilder sb = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
sb.insert(0, Long.toString(ip & 0xff));
if (i < 3) {
sb.insert(0, '.');
}
ip >>= 8;
}
return sb.toString();
}
}
另一个将 IP 转换为十进制的 JAVA 示例:
-
字符串 ip="70.30.65.9";
String[] addrArray = ip.split("\.");
长数 = 0;
对于 (int i = 0; 我
将IP转换为十进制的PHP函数:
-
功能 myip2long($ip) {
if (is_numeric($ip)) {
返回 sprintf("%u", floatval($ip));
} 别的 {
返回 sprintf("%u", floatval(ip2long($ip)));
}
}
尝试应用它,例如:
回声 myip2long("192.168.1.1");
PHP 中将 IP 转换为十进制的另一个示例:
-
函数 myip2long2($ip){
$d = 0.0;
$b = explode(".", $ip,4);
对于 ($i = 0; $i
将十进制转换为 IP 的 PHP 示例:
-
函数 customLong2ip($ip){
$b=数组(0,0,0,0);
$c = 16777216.0;
$ip += 0.0;
对于 ($i = 0; $i