Human readable DhcpInfo.ipAddress?

后端 未结 5 2144
臣服心动
臣服心动 2021-02-15 10:31

I am wondering how to get a human readable IP Adress from DhcpInfo.ipAddress? The tricky thing about it is, that it is an integer and obviously you can\'t store an IP address in

5条回答
  •  暖寄归人
    2021-02-15 11:23

    As mentioned by other posters, an ip address is 4 bytes that can be packed in to one int. Andrey gave a nice illustration showing how. If you store it in an InetAddress object you can use ToString() to get the human readable version. Something like:

    byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
    InetAddress address = InetAddress.getByAddress(bytes);
    String s = address.ToString();
    

提交回复
热议问题