Human readable DhcpInfo.ipAddress?
Asked Answered
O

5

8

I am wondering how to get a human readable IP Adress from DhcpInfo.ipAddress? The tricky thing about it is, that it is an integer and obviously you can't store an IP address in an integer. So, how is the IP address encoded, that it can be stored in an int? The documentation doesn't give any help to this problem: http://developer.android.com/reference/android/net/DhcpInfo.html#ipAddress Thanks :-)

Orman answered 14/6, 2011 at 14:58 Comment(0)
E
8

You can actually.

IP address as int is: AABBCCDD and in human-readable form it is AA.BB.CC.DD but in decimal base. As you see you can easily extract them using bitwise operations or by converting int to byte array.

See the picture:

enter image description here

Ecclesiastes answered 14/6, 2011 at 15:2 Comment(1)
awesome graphic illustrating this, thank you! just wished, it would be included in the android documentation.Orman
G
8

Use this function NetworkUtils.java \frameworks\base\core\java\android\net)

public static InetAddress intToInetAddress(int hostAddress) {
    byte[] addressBytes = { (byte)(0xff & hostAddress),
                            (byte)(0xff & (hostAddress >> 8)),
                            (byte)(0xff & (hostAddress >> 16)),
                            (byte)(0xff & (hostAddress >> 24)) };

    try {
       return InetAddress.getByAddress(addressBytes);
    } catch (UnknownHostException e) {
       throw new AssertionError();
    }
}
Godoy answered 5/6, 2012 at 4:40 Comment(0)
S
5

obviously you can't store an IP address in an integer

Actually, that's all an IP (v4) address is -- a 32-bit integer (or 128-bit, in the case of IPv6).

The "human-readable" format you're talking about is produced by dividing the bits of the integer into groups of 8 called "octets" and converting to base 10, e.g. "192.168.0.1".

The bits of this address would be as follows (spaces added for readability):

11000000 10101000 00000000 00000001

Which corresponds to the decimal integer 3,232,235,521.

Stancil answered 14/6, 2011 at 15:1 Comment(1)
thank you, I am obviously too used to the human readable format ;-)Orman
S
5

As mentioned by other posters, an ip address is 4 bytes that can be packed in to one int. Andrey gave a nice illustration showing how. If you store it in an InetAddress object you can use ToString() to get the human readable version. Something like:

byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
InetAddress address = InetAddress.getByAddress(bytes);
String s = address.ToString();
Slink answered 14/6, 2011 at 15:14 Comment(1)
Unfortunately the IP Address is in the wrong order, e.g. 192.168.101.102 will be output as 102.101.168.192. At least using WifiInfo.getIPAddress(). Use bytes conversion of (answered by) Ace instead.Vadnee
N
0

just reverse the ipaddress which you receive in bytes

byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
ArrayUtils.reverse(bytes);
// then
InetAddress myaddr = InetAddress.getByAddress(ipAddress);
String ipString = myaddr.getHostAddress();
Nato answered 14/11, 2014 at 7:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.