Wednesday, 24 August 2016

[Java] Convert Hexadeciamal to IP address

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public static String convertHex2IP( String hex) throws UnknownHostException {
 java.net.InetAddress address = java.net.InetAddress.getByAddress( javax.xml.bind.DatatypeConverter.parseHexBinary( hex));
 return address.getHostAddress();
  
}
 
public static String convertIP2Hex( String ip) throws UnknownHostException {
 java.net.InetAddress address = java.net.InetAddress.getByName( ip);
 byte[] bytes = address.getAddress();
 StringBuilder sb = new StringBuilder();
 for ( int ll = 0; ll < bytes.length; ll++)
  sb.append( String.format( "%02X", bytes[ ll]));
 
 return sb.toString();
  
}


No comments:

Post a Comment