Reputation: 1402
I've got a problem. I'm displaying the network info in my application in this way:
// 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);
all of course using wifiManager
. Now i have a method to convert the values
public String intToIp(int i) {
return ((i >> 24 ) & 0xFF ) + "." +
((i >> 16 ) & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
( i & 0xFF) ;
}
It works.. But instead displays: 192.168.0.0
it displays 0.0.168.192
.. How can i solve?
Upvotes: 0
Views: 423
Reputation: 11943
Basically you already solved it. The bytes are arranged in little endian notation, meaning that the most significant byte (192) is at the last position. What you expect is big endian, which is used by "the humans".
You might read about Endianness here.
Just reverse the order in which you extract each byte and you're done.
Upvotes: 0
Reputation: 688
Just invert your intToIp method:
public String intToIp(int i) {
return (i & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
((i >> 16) & 0xFF) + "." +
((i >> 24) & 0xFF);
}
Upvotes: 2