ip的轉化

public void getAddress() {     
        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);     
        WifiInfo info = wifi.getConnectionInfo();
        Log.i("size", "IP: "+convertIpaddr2String(info.getIpAddress()));
        Log.i("size", "MAC: "+info.getMacAddress());
    }  
    
    public static String convertIpaddr2String(int ip_int){  
        
        byte[] bytes = convertInt2Bytes(ip_int);  
        InetAddress addr = null;  
        try {  
            addr = InetAddress.getByAddress(bytes);  
        } catch (UnknownHostException e) {  
            e.printStackTrace();  
            return null;  
        }  
        return addr.getHostAddress();  
    }  
    public static byte[] convertInt2Bytes(int value){  
        
        byte[] addr = new byte[4];  
        addr[3] = (byte) ((value >>> 24) & 0xFF);  
        addr[2] = (byte) ((value >>> 16) & 0xFF);  
        addr[1] = (byte) ((value >>> 8) & 0xFF);  
        addr[0] = (byte) (value & 0xFF);  
        return addr;  
    }  

int轉為String

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章