package com.awkj; import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; public class App { private static BigInteger MIN_IP = new BigInteger("0"); private static BigInteger MAX_IP = new BigInteger("4294967295"); static String inet_ntoa(BigInteger raw) { int minFlag = raw.compareTo(MIN_IP); int maxFlag = raw.compareTo(MAX_IP); if (minFlag < 0 || maxFlag > 0) { return null; } else { try { long rawLong = raw.longValue(); byte[] b = new byte[]{(byte) (rawLong >> 24), (byte) (rawLong >> 16), (byte) (rawLong >> 8), (byte) rawLong}; return InetAddress.getByAddress(b).getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); return null; } } } public static void main(String[] args) { /* MYSQL result: SELECT INET_NTOA(-1); --> NULL SELECT INET_NTOA(0); --> 0.0.0.0 SELECT INET_NTOA(134744072); --> 8.8.8.8 SELECT INET_NTOA(4294967295); --> 255.255.255.255 SELECT INET_ATON(4294967296); --> NULL */ System.out.println(inet_ntoa(new BigInteger("-1"))); System.out.println(inet_ntoa(new BigInteger("0"))); System.out.println(inet_ntoa(new BigInteger("134744072"))); System.out.println(inet_ntoa(new BigInteger("4294967295"))); System.out.println(inet_ntoa(new BigInteger("4294967296"))); } }