您有不正确的值,70.30.65.9将与1176387849对应,但永远不会与9766322441对应。
C++将IP转换为十进制的示例:
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';
}
这是一个用JAVA编写的将IP地址转换为十进制数的类:-
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("\.");
long num = 0;
for (int i = 0; i
将IP转换为十进制的PHP函数:
-
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");
另一个将IP转换为十进制的PHP示例:
-
function myip2long2($ip){
$d = 0.0;
$b = explode(".", $ip,4);
for ($i = 0; $i
将十进制数转换回IP的PHP示例:
-
function customLong2ip($ip){
$b=array(0,0,0,0);
$c = 16777216.0;
$ip += 0.0;
for ($i = 0; $i