IP address conversion to decimal and vice versa
Solution 1
IPv4 addresses are on 32 bit.
The number 9,766,322,441
cannot be represented on 32 bit (max: 4,294,967,295
).
Solution 2
You have incorrect values 70.30.65.9 will correspond with 1176387849 but never with 9766322441.
Limitation of 32 bits - Java: how to convert dec to 32bit int?
C++ Example of converting IP to decimal:
#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';
}
This is JAVA class to convert IP to decimal: -
/**
* @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();
}
}
Another JAVA example to convert IP to decimal: - String ip="70.30.65.9"; String[] addrArray = ip.split("\."); long num = 0; for (int i = 0; i PHP function to convert IP to decimal: - function myip2long($ip) { if (is_numeric($ip)) { return sprintf( "%u", floatval($ip) ); } else { return sprintf( "%u", floatval(ip2long($ip) )); } } Try applying it, e.g.: echo myip2long("192.168.1.1");
Another example in PHP to convert IP to decimal: - function myip2long2($ip){ $d = 0.0; $b = explode(".", $ip,4); for ($i = 0; $i PHP example to convert decimal to IP: - function customLong2ip($ip){ $b=array(0,0,0,0); $c = 16777216.0; $ip += 0.0; for ($i = 0; $i
Solution 3
You are exceeding the valid IPV4 range with the decimal value of 9766322441. If translated to Hex, it gives you the value 0x2461E4109 that occupies 5 octets instead of 4:
02, 46, 1e, 41, 09
The calculator simply truncates the octet with the value of 0x02 and converts the value using the 4 least significant octets.
The calculations for the 0x461E4109 gives you the "70.30.65.9" which is shown by the calculator.
How to do this in Java:
I recommend using standard Java classes for InetAddress manipulations. This should work both with IPV4 and IPv6: http://docs.oracle.com/javase/1.5.0/docs/api/java/net/InetAddress.html
You may be looking on something like (I used Java more than 10 years ago, so feel free to correct syntax errors if you find them)
InetAddress Address = InetAddress.getByName("192.168.0.1");
InetAddress Address = InetAddress.getByName(raw2);
String s1 = Address.toString();
byte[] raw = Adress.getAddress();
byte[] rawIPv4 = {192, 168, 0, 1};
InetAddress Address1 = InetAddress.getByName(rawIPV4);
InetAddress Address2 = InetAddress.getByName("2607:f0d0:1002:51::4");
byte[] rawIPV6 = Adress.getAddress();
The IPv6 example also covers the case of the "compressed zeros" in the address (the "::" between 51 and 4).
Anyway, you may want to check this for converting an array of bytes to integer:
Byte Array and Int conversion in Java
Please note, however, that the IPV6 format does use 16 bytes for a single IP address, so I am not sure if you can represent it with a single integer value as shown in the calculator.
Solution 4
In reading the original post, and the subsequent follow up posts in response to people's answers, I get the distinct impression that your original "10 digit decimal" is actually a phone number that you want to somehow turn into an IP address.
"because i want to develop the algorithm for conversion of mobile to ip and vice versa"
It doesn't work that way. That's like asking how to take a MAC address and converting it to an IP address. Phone numbers and IP addresses are allocated by two completely different authorities. There is no algorithm to relate the two.
akshay1728
Updated on May 08, 2021Comments
-
akshay1728 almost 3 years
Suppose my decimal number is
9766322441
so its corresponding is70.30.65.9
but when this IP address IC converted back, its gives some different decimal number1176387849
... and when I convert the IP address pf google.com i.e64.233.187.99
then it gives the1089059683
and reverse conversion gives the correct IP address i.e64.233.187.99
...My question is what is wrong with the the above mention number? I also try with
9579342332
but the same result. It gives the wrong reverse conversion??What is the reason behind it??
You could use this calculator for calculations.
-
Francisco Zarabozo almost 3 yearsBased on the comments of some answers, the OP is trying to convert a phone number into an IP address, which is impossible. Those are two different, completely unrelated values.
-
-
akshay1728 over 11 yearsfrom decimal to ip??and why the above numbers that i mention dont give the same conversion
-
akshay1728 over 11 yearsthanx for anwering i got it..then what will be the steps to convert it back to that number because i am designing the algorithm for it
-
akshay1728 over 11 yearsok the any solution to convert that for this...because i want to develop the algorithm for conversion of mobile to ip and vice versa..
-
akshay1728 over 11 yearssir, i want the general solution in c, java
-
Vincenzo Pii over 11 years@PriyaSakal you should check the validity of the input,
9.766.322.441
is not a valid IP address. -
akshay1728 over 11 yearsit means there is no solution to convert any 10 digit number to ip and vice versa..there is limitation that you have mention
-
Ilia over 11 yearsOh, I'm very sorry, didn't notice this, I thought that was PHP!
-
Vincenzo Pii over 11 years@PriyaSakal of course there's a limitation, can you represent the decimal number 7 with 2 bits? No! With two bits you can represent 0, 1, 2 and 3. With IPv4 addresses you only have 32 bits, you have to comply with this.
-
akshay1728 over 11 yearsbut sir there is limitation of 32 bits...9766322441 cannot be represented in ip address as others are saying... i want the coversion for both mobile number to ip and ip to mobile number
-
Ilia over 11 yearsPlease read then: stackoverflow.com/questions/4984130/…
-
user207421 over 11 yearsIt would be a lot clearer if you wrote
a <<= 24; b <<= 16; c <<= 8;
. IP addresses aren't calculated by multiplication, they are the result of shifting and bitmasking, so don't use multiplications in your code. -
MarkHu over 11 yearsWhat about PHP's built-in ip2long() and long2ip() functions?
-
mvreijn almost 9 yearsPlease note that the
main()
method in your Java class example is wrong - the radix should be 10 instead of 16. Also, just usingLong.parseLong( String str )
works fine. -
mvreijn almost 9 years@IliaRostovtsev I did, you're welcome to accept it :-D
-
Ilia almost 9 years@mvreijn I see no pull-requests?
-
Francisco Zarabozo almost 3 yearsThe max value for an IP address is 255.255.255.255, not 256.256.256.256.