Search code examples
javamysqlinetaddressinet-aton

ISSUE: java.net.InetAddress RESULTS are wrong when compared with MYSQL results


I've followed the answer in https://stackoverflow.com/a/2241269/2458223 (which William Brendel suggested) but most of the time returns wrong results when i compare the results with MYSQL. Please review

InetAddress bar = InetAddress.getByName("187.210.139.0");
value = ByteBuffer.wrap(bar.getAddress()).getInt();

results you can view here...

JAVA Results: (Refer: http://ideone.com/CJ3qCU )

returns: 1143829760

MYSQL Results:

mysql> select inet_aton("187.210.139.0");
+----------------------------+
| inet_aton("187.210.139.0") |
+----------------------------+
|                 3151137536 |
+----------------------------+

What is issue here.. any bug?? Please help!!


Solution

  • Isn't the result in Java -1143829760? not 1143829760?

    Anyway, in Java,

    wrap(bar.getAddress()).getInt();

    It returns "signed int", and in mysql,

    inet_aton();

    It returns "unsigned int".

    Ip address "187.210.139.0"can be converted into binary number is :

    1011 1011(187). 1101 0010(210). 1000 1011(139). 0000 0000(0)

    These function and method read this removing dots(.) after converting IP into binary number.

    And they calculate that address.

    It's the reason why "inet_aton()" returns larger number than "wrap(bar.getAddress()).getInt()";