Java的inet_aton inet_ntoa

来源:互联网 发布:淘宝关联销售模板 编辑:程序博客网 时间:2024/05/01 21:50
public static String inet_ntoa(long add) {return ((add & 0xff000000) >> 24) + "." + ((add & 0xff0000) >> 16)+ "." + ((add & 0xff00) >> 8) + "." + ((add & 0xff));}public static long inet_aton(Inet4Address add) {byte[] bytes = add.getAddress();long result = 0;for (byte b : bytes) {if ((b & 0x80L) != 0) {result += 256L + b;} else {result += b;}result <<= 8;}result >>= 8;return result;}/** * significantly faster than parse the string into long */public static long inet_aton(String add) {long result = 0;// number between a dotlong section = 0;// which digit in a numberint times = 1;// which sectionint dots = 0;for (int i = add.length() - 1; i >= 0; --i) {if (add.charAt(i) == '.') {times = 1;section <<= dots * 8;result += section;section = 0;++dots;} else {section += (add.charAt(i) - '0') * times;times *= 10;}}section <<= dots * 8;result += section;return result;}
没有找到好的java版,在大规模使用的时候还是有些差距的。