2014-01-09 61 views
0

我遇到了问题。我以这种方式在我的应用程序中显示网络信息:网络信息android ip倒置问题

// Some wifi info 
     d=wifiManager.getDhcpInfo(); 

     s_dns1="DNS 1: "+intToIp(d.dns1); 
     s_dns2="DNS 2: "+intToIp(d.dns2);  
     s_gateway="Default Gateway: "+intToIp(d.gateway);  
     s_ipAddress="IP Address: "+intToIp(d.ipAddress); 
     s_leaseDuration="Lease Time: "+String.valueOf(d.leaseDuration);  
     s_netmask="Subnet Mask: "+intToIp(d.netmask);  
     s_serverAddress="Server IP: "+intToIp(d.serverAddress); 

当然全部使用wifiManager。现在我要值

public String intToIp(int i) { 

     return ((i >> 24) & 0xFF) + "." + 
      ((i >> 16) & 0xFF) + "." + 
      ((i >> 8) & 0xFF) + "." + 
      (i & 0xFF) ; 
     } 

它可以转换的方法..而是显示:192.168.0.0它显示0.0.168.192 ..我该如何解决?

+0

除了将订单更改为i,i >> 8等? – user3125280

+0

另请参阅http://developer.android.com/reference/android/text/format/Formatter.html#formatIpAddress(int) – laalto

回答

2

只需翻转你的intToIp方法:

public String intToIp(int i) { 
    return (i & 0xFF) + "." + 
     ((i >> 8) & 0xFF) + "." + 
     ((i >> 16) & 0xFF) + "." + 
     ((i >> 24) & 0xFF); 
} 
+0

非常感谢您 –

0

基本上你已经解决了这个问题。字节以小端符号排列,这意味着最高有效字节(192)位于最后位置。你所期望的是“人类”使用的大端。

你可能会读到关于排序here

只是逆转你提取每个字节的顺序,你就完成了。