您的值不正确 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 示例: - String ip="70.30.65.9"; String[] addrArray = ip.split("\."); 长数 = 0; for (int i = 0; i PHP 函数将 IP 转换为十进制: - function myip2long($ip) { if (is_numeric($ip)) { return sprintf( "%u", floatval($ip) ); } else { return sprintf( "%u", floatval(ip2long($ip) )); } } 尝试应用它,例如: echo myip2long("192.168.1.1");
PHP 中将 IP 转换为十进制的另一个示例: - function myip2long2 ($ip){ $d = 0.0; $b = explode(".", $ip,4); for ($i = 0; $i PHP 示例将十进制转换为 IP: - function customLong2ip($ip){ $b=数组(0,0,0,0);$c = 16777216.0;$ip += 0。
0; 对于 ($i = 0; $i