java - getHostAddress() returns a reversed ip address -


i'm trying cell phone ip address using wifimanager , wifiinfo classes.

it returns correct ip address reversed.

public string getwifiipaddress() {     wifimanager wm = (wifimanager) getsystemservice(wifi_service);     wifiinfo wi = wm.getconnectioninfo();      byte[] ipaddress = biginteger.valueof(wi.getipaddress()).tobytearray();     try {         inetaddress myaddr = inetaddress.getbyaddress(ipaddress);         string hostaddr = myaddr.gethostaddress();         return hostaddr;     } catch (unknownhostexception e) {         // todo auto-generated catch block         e.printstacktrace();     }      return ""; } 

result : 73.0.168.192

ok, saw address reversed! :)

it referred big/little endian issue, read more endianness must-to-know programmers, specially when doing applications integrations , migrations on different operating systems.

add after gettting connection info wifi manager.

int ipaddress = wi.getipaddress();  ipaddress = (byteorder.nativeorder().equals(byteorder.little_endian)) ?                  integer.reversebytes(ipaddress) : ipaddress; 

then continue code tobytearray , gethostaddress etc.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -