I'm developing an application to turn off PCs in a lab. I have a reference to class InetAddress
, invoking method getLocalhost()
. Then I set this to a% byte of% and then I made a array
to display the IP in sequence.
However, since it is a% byte of bytes, if the address is as in my case: 10.248.72.58, it displays as follows: 10.-8.72.58, being byte, the range of numbers that " fit "runs from -128 to 127 (8 bits).
Only if I change to for
, the error at the time of calling the function (which returns byte), so array
must be byte.
InetAddress localhost = InetAddress.getLocalHost(); // Pega o nome do host do sistema,
//resolvendo pra um objeto InetAddress. Faz cache do endereço por um curto periodo de tempo
byte[] ip = localhost.getAddress();
for (int i = 0; i < 4; i++) {
System.out.println("Ip: " + (byte)ip[i]);
}
Output: Ip: 10.-8.72.58