package com.lhy; public class IpUtil { public static void main(String[] args) { //124.160.217.97 //49.93.207.191 String ip = "49.93.207.191"; System.out.println(ipStrToLong(ip)); long longIp = 828231615; System.out.println(ipLongToStr(longIp)); } public static long ipStrToLong(String strIp){ String[] arrIp = strIp.split("\\."); return (Long.parseLong(arrIp[0])<< 24) + (Long.parseLong(arrIp[1])<<16) + (Long.parseLong(arrIp[2])<<8) + (Long.parseLong(arrIp[3])); } public static String ipLongToStr(long longIP){ StringBuffer sb = new StringBuffer(); sb.append(longIP >>> 24) .append(".") .append((longIP & 0x00FFFFFF) >>> 16) .append(".") .append((longIP & 0x00FFFF) >>> 8) .append(".") .append((longIP & 0x00FF)); return sb.toString(); } }