不知道如何整数转换成IP的Andr​​oid和Java [英] Cannot understand how to convert integer to IP in Android and Java

查看:150
本文介绍了不知道如何整数转换成IP的Andr​​oid和Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了我的Andr​​oid手机的小型应用程序。我在没有计算机科学背景
但我熟悉C / C ++和一些发展理念。现在我是想显示的IP地址
我的手机。并根据Android的引用我用它返回 getIpAddress()方法
一个整数。

然后查看计算器我看到的解决方案张贴在这里:
Android的IP地址与Java
它被张贴保罗·弗格森和我在这里将其复制:

 字符串ipString =的String.format(
%d个,%d个,%d个%D,
(IP&安培;为0xFF)
(IP>→8&放大器; 0xff的),
(IP>> 16安培;为0xFF)
(IP>> 24安培;为0xFF));

IP 的类型为 INT 包含IP地址的变量。任何人都可以请向我解释这是如何工作?请注意,我的Java技能都相当的介绍。

多亏了@Joop·埃根的意见可以说,我们有 -926414400 的IP地址整数这里是code如何工作的:

  IP&个行业。1 GT; 。 2> 。 &所述3的密度; 。 &所述; 4为H.一切都使用位(和)操作完成这件事
也就是说,所有的操作都​​是由垂直位位进行场< 1 GT;ipint = 11001000 11001000 00001001 11000000
为0xFF = 00000000 00000000 00000000 11111111
           ------------------------------------
&所述1为卤素; = 00000000 00000000 00000000 11000000它等于192 字段2>首先我们做了8位因为换档
 在>>运营商需要precedence在&放大器;操作者
 同为场3;>和< 4为H.由8位ipint现在成为移ipint = 00000000 11001000 11001000 00001001
为0xFF = 00000000 00000000 00000000 11111111
           ------------------------------------
2> = 00000000 00000000 00000000 00001001其等于9对于现场3;>由16位ipint成为转移ipint = 00000000 00000000 11001000 11001000
为0xFF = 00000000 00000000 00000000 11111111
           ------------------------------------
&所述3的密度; = 00000000 00000000 00000000 11001000这是200
对于现场4;>由24位ipint成为转移ipint = 00000000 00000000 00000000 11001000
为0xFF = 00000000 00000000 00000000 11111111
           ------------------------------------
&所述; 4为H. = 00000000 00000000 00000000 11001000这是200因此IP为192.9.200.200


解决方案

有两个标准, IP V4 使用4个字节,而较新的仍然不是太宽US $ p $垫 IP v6的与多个字节。

现在在你的IP v4的情况下,4个字节可以包括一个Java整数或只是每个字节上市。

顺便说一句网址可以使用IP号码既作为4字节值,或者作为一个单独的整数:

 的http:// 3232235778 /
http://210.43.98.51/

(我没有转换或检查数字。)

在code使用的伎俩,整型转换为长,因为Java整数签署;最左边的比特是1并不意味着一个非常高的数字,但负

要取出的4个字节,每字节8位,它们被掩蔽并向右移动。其中,将0xFF FF在基数为16,255基地10,0b11111111或11111111在基地2个。

有一个位操作和放大器; (和),这是实际上是乘法(模2)。

  X Y X&安培; Y X | Y
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1

现在有一些位 ABCDEFGH ,并要检查仅位 DEF 你会怎么做:

  ABCDEFGH&安培; 00011100 = 000def00 = def00(00011100称为掩模)
(ABCDEFGH&安培; 00011100)GT;> 2 =高清(右移2)

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

解决方案

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)

这篇关于不知道如何整数转换成IP的Andr​​oid和Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆