Human readable DhcpInfo.ipAddress?

后端 未结 5 2168
臣服心动
臣服心动 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条回答
  •  猫巷女王i
    2021-02-15 11:07

    Use this function NetworkUtils.java \frameworks\base\core\java\android\net)

    public static InetAddress intToInetAddress(int hostAddress) {
        byte[] addressBytes = { (byte)(0xff & hostAddress),
                                (byte)(0xff & (hostAddress >> 8)),
                                (byte)(0xff & (hostAddress >> 16)),
                                (byte)(0xff & (hostAddress >> 24)) };
    
        try {
           return InetAddress.getByAddress(addressBytes);
        } catch (UnknownHostException e) {
           throw new AssertionError();
        }
    }
    

提交回复
热议问题