3

Suppose my decimal number is 9766322441 so its corresponding is 70.30.65.9 but when this IP address IC converted back, its gives some different decimal number 1176387849... and when I convert the IP address pf google.com i.e 64.233.187.99 then it gives the 1089059683 and reverse conversion gives the correct IP address i.e 64.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.

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
akshay1728
  • 373
  • 2
  • 6
  • 15
  • Based 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. – Francisco Zarabozo May 08 '21 at 07:25

7 Answers7

19

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
Francisco Zarabozo
  • 3,676
  • 2
  • 28
  • 54
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
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.

Community
  • 1
  • 1
Alex
  • 389
  • 2
  • 12
  • thanx 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 Aug 26 '12 at 13:46
2

IPv4 addresses are on 32 bit.

The number 9,766,322,441 cannot be represented on 32 bit (max: 4,294,967,295).

Mikael Östberg
  • 16,982
  • 6
  • 61
  • 79
Vincenzo Pii
  • 18,961
  • 8
  • 39
  • 49
  • ok 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 Aug 26 '12 at 13:49
  • 1
    @PriyaSakal you should check the validity of the input, `9.766.322.441` is not a valid IP address. – Vincenzo Pii Aug 26 '12 at 13:51
  • it means there is no solution to convert any 10 digit number to ip and vice versa..there is limitation that you have mention – akshay1728 Aug 26 '12 at 13:53
  • 1
    @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. – Vincenzo Pii Aug 26 '12 at 13:59
1

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.

0

Actually IP addresses CAN be converted to 32 bit java integers its just that the number might be negative. That is really ok because you can converted it back and forth without data loss. Now if you really want to use an extra 32 bits and store it in a long just so it does not look negative to java you can, but its wasteful. You can write a function to cast it to long and display it unsigned but store it in and int.

0

There are also some ways to do it in Excel. some of the calculations are messy but the simplest one is to use a User Defined Function as seen in this post How to Sort IP Address Lists in Excel

-1

The question was why did 9766322441 convert to 70.30.65.9 but when converted back it gives 1176387849 instead of 9766322441. The answer is because 4294967296 is the highest possible decimal number represented by the IP 256.256.256.256.

Above 4294967296 it will convert to a number below 256.256.256.256 on the return trip just like rolling the odometer on your car. 9766322441 is clearly more than twice the value of 4294967296 and ends up rolling the odometer twice ending up with 1176387849 on the third roll of the odometer. This new number will then convert to and from an IP address normally as it is within range.