aks*_*728 2 php java networking decimal ip-address
假设我的十进制数是9766322441如此相应,70.30.65.9但是当这个IP地址IC转换回来时,它会给出一些不同的十进制数1176387849......当我转换IP地址时,请将gof.com转换为ie,64.233.187.99然后它给出了1089059683反向转换,给出了正确的IP地址即64.233.187.99......
我的问题是上面提到的数字有什么问题?我也尝试9579342332但结果相同.它给出了错误的反向转换??
它背后的原因是什么?
您可以使用此计算器进行计算.
Ili*_*sev 18
您有不正确的值70.30.65.9将对应1176387849但从不与9766322441对应.
#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';
}
Run Code Online (Sandbox Code Playgroud)
/**
* @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();
}
}
Run Code Online (Sandbox Code Playgroud)
String ip="70.30.65.9";
String[] addrArray = ip.split("\.");
long num = 0;
for (int i = 0; i < addrArray.length; i++) {
int power = 3 - i;
num += ((Integer.parseInt(addrArray[i]) % 256 * Math.pow(256, power)));
}
Run Code Online (Sandbox Code Playgroud)
function myip2long($ip) {
if (is_numeric($ip)) {
return sprintf( "%u", floatval($ip) );
} else {
return sprintf( "%u", floatval(ip2long($ip) ));
}
}
Run Code Online (Sandbox Code Playgroud)
尝试应用它,例如:
echo myip2long("192.168.1.1");
Run Code Online (Sandbox Code Playgroud)
function myip2long2($ip){
$d = 0.0;
$b = explode(".", $ip,4);
for ($i = 0; $i < 4; $i++) {
$d *= 256.0;
$d += $b[$i];
};
return $d;
}
Run Code Online (Sandbox Code Playgroud)
这个将为您提供与上面相同的结果:
function customLong2ip($ip){
$b=array(0,0,0,0);
$c = 16777216.0;
$ip += 0.0;
for ($i = 0; $i < 4; $i++) {
$k = (int) ($ip / $c);
$ip -= $c * $k;
$b[$i]= $k;
$c /=256.0;
};
$d=join('.', $b);
return($d);
}
Run Code Online (Sandbox Code Playgroud)