问题
I am building a small application for my android phone. My background in not on computer science
but I am familiar with C/C++ and some development concepts. Now I was trying to display the IP address
of my phone. And according to the Android references I used the getIpAddress()
method which returns
an Integer.
Then Looking in stackoverflow I saw the solution posted here:
Android IP address with java
which was posted by Paul Ferguson
and I am copying it here:
String ipString = String.format(
"%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));
the ip
is a variable of type int
that contains the ip address. Can anyone please explain to me how this works? Note that my java skills are quite introductory.
Thanks to the comments by @Joop Eggen lets say we have an ip address integer of -926414400
Here is how the code works:
IP fileds <1> . <2> . <3> . <4>
everything is done using the bitwise (and) operation this
means that all operations are made vertically bit by bit
field <1>
ipint = 11001000 11001000 00001001 11000000
0xff = 00000000 00000000 00000000 11111111
------------------------------------
<1> = 00000000 00000000 00000000 11000000
which is equal to 192
Field <2> first we do a shift by 8 bits since
the >> operator takes precedence over the & operator
same for fields <3> and <4>
shift by 8 bits ipint now becomes
ipint = 00000000 11001000 11001000 00001001
0xff = 00000000 00000000 00000000 11111111
------------------------------------
<2> = 00000000 00000000 00000000 00001001
which is equal to 9
For field <3> shift by 16 bits ipint becomes
ipint = 00000000 00000000 11001000 11001000
0xff = 00000000 00000000 00000000 11111111
------------------------------------
<3> = 00000000 00000000 00000000 11001000
which is 200
For field <4> shift by 24 bits ipint becomes
ipint = 00000000 00000000 00000000 11001000
0xff = 00000000 00000000 00000000 11111111
------------------------------------
<4> = 00000000 00000000 00000000 11001000
which is 200
So the ip is 192.9.200.200
回答1:
There are two standards, IP v4 using 4 bytes, and the newer still not too wide spread IP v6 with more bytes.
Now in your IP v4 case, the 4 bytes may comprise a Java integer or simply every byte listed.
By the way URLs may use the IP number both as 4 byte values, or as one single integer:
http://3232235778/
http://210.43.98.51/
(I did not convert or check the numbers.)
The code uses a trick, converting the int to long, as java integers are signed; the left-most bit being 1 does not mean a very high number, but negative.
To take out the 4 bytes, every byte 8 bits, they are masked and shifted to the right. Where 0xFF is FF in base 16, 255 in base 10, 0b11111111 or 11111111 in base 2.
There is a bit operation & ("and") which is in fact is multiplication (modulo 2).
x y x&y x|y
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
Now having some bits abcdefgh
and you want to inspect only bits def
you would do:
abcdefgh & 00011100 = 000def00 = def00 (00011100 is called a mask)
(abcdefgh & 00011100) >> 2 = def (shift right 2)
回答2:
We show you two bit shifting and “0xff” masking examples to convert a decimal number back to IP address. The bit shifting is very hard to explain in words, it’s better review the binary flows below :
//ip = 3232235778
public String longToIp(long ip) {
StringBuilder result = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
result.insert(0,Long.toString(ip & 0xff));
if (i < 3) {
sb.insert(0,'.');
}
ip = ip >> 8;
}
return result.toString();
}
for more details please check the Link Link 2
来源:https://stackoverflow.com/questions/27170602/cannot-understand-how-to-convert-integer-to-ip-in-android-and-java