@Test public void shouldConvertIpString2Long() throws IPStringConversionException, IPOctetOutOfRangeException { assertEquals(3232235520L, IPv4ToolSet.ip2long("192.168.0.0")); }
public static long ip2long(String ip) throws IPStringConversionException, IPOctetOutOfRangeException { long out = 0; String ippatternstr = "^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$"; Pattern ipPattern = Pattern.compile(ippatternstr); Matcher ipMatch = ipPattern.matcher(ip); String parseError = String.format("Could not parse IP string \"%s\"", ip); int i = 1; if (ipMatch.find()) { try { for (i = 1; i <= 4; i++) { long octet = Long.parseLong(ipMatch.group(i)); if (octet < 0 || octet > 255) { String errMsg = String.format("Octet %s out of range 0-255 in ip %s", ipMatch.group(i), ip); throw new IPOctetOutOfRangeException(errMsg); } out += octet << (8 * -i + 32); } } catch (NumberFormatException e) { String errMsg = String.format("Could not convert %s to integer in ip \"%s\"", ipMatch.group(i), ip); throw new IPStringConversionException(errMsg); } } else { throw new IPStringConversionException(parseError); } return out; }
IPv4ToolSet { public static long ip2long(String ip) throws IPStringConversionException, IPOctetOutOfRangeException { long out = 0; String ippatternstr = "^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$"; Pattern ipPattern = Pattern.compile(ippatternstr); Matcher ipMatch = ipPattern.matcher(ip); String parseError = String.format("Could not parse IP string \"%s\"", ip); int i = 1; if (ipMatch.find()) { try { for (i = 1; i <= 4; i++) { long octet = Long.parseLong(ipMatch.group(i)); if (octet < 0 || octet > 255) { String errMsg = String.format("Octet %s out of range 0-255 in ip %s", ipMatch.group(i), ip); throw new IPOctetOutOfRangeException(errMsg); } out += octet << (8 * -i + 32); } } catch (NumberFormatException e) { String errMsg = String.format("Could not convert %s to integer in ip \"%s\"", ipMatch.group(i), ip); throw new IPStringConversionException(errMsg); } } else { throw new IPStringConversionException(parseError); } return out; } }
IPv4ToolSet { public static long ip2long(String ip) throws IPStringConversionException, IPOctetOutOfRangeException { long out = 0; String ippatternstr = "^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$"; Pattern ipPattern = Pattern.compile(ippatternstr); Matcher ipMatch = ipPattern.matcher(ip); String parseError = String.format("Could not parse IP string \"%s\"", ip); int i = 1; if (ipMatch.find()) { try { for (i = 1; i <= 4; i++) { long octet = Long.parseLong(ipMatch.group(i)); if (octet < 0 || octet > 255) { String errMsg = String.format("Octet %s out of range 0-255 in ip %s", ipMatch.group(i), ip); throw new IPOctetOutOfRangeException(errMsg); } out += octet << (8 * -i + 32); } } catch (NumberFormatException e) { String errMsg = String.format("Could not convert %s to integer in ip \"%s\"", ipMatch.group(i), ip); throw new IPStringConversionException(errMsg); } } else { throw new IPStringConversionException(parseError); } return out; }  }
IPv4ToolSet { public static long ip2long(String ip) throws IPStringConversionException, IPOctetOutOfRangeException { long out = 0; String ippatternstr = "^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$"; Pattern ipPattern = Pattern.compile(ippatternstr); Matcher ipMatch = ipPattern.matcher(ip); String parseError = String.format("Could not parse IP string \"%s\"", ip); int i = 1; if (ipMatch.find()) { try { for (i = 1; i <= 4; i++) { long octet = Long.parseLong(ipMatch.group(i)); if (octet < 0 || octet > 255) { String errMsg = String.format("Octet %s out of range 0-255 in ip %s", ipMatch.group(i), ip); throw new IPOctetOutOfRangeException(errMsg); } out += octet << (8 * -i + 32); } } catch (NumberFormatException e) { String errMsg = String.format("Could not convert %s to integer in ip \"%s\"", ipMatch.group(i), ip); throw new IPStringConversionException(errMsg); } } else { throw new IPStringConversionException(parseError); } return out; }  static String long2ip(long ipLong); static long mask_bits(int bits); static List<String> ipv4BlockToIpStrings(String blockString); static List<String> ipv4BlocksToIpStrings(List<String> ipBlocks); static IPv4Range ipv4BlockToRange(String blockString); static boolean isValid(String ip); static long ip2long(String ip); static boolean rejectUnwantedIps(String ip); }
IPv4ToolSet { public static long ip2long(String ip) throws IPStringConversionException, IPOctetOutOfRangeException { long out = 0; String ippatternstr = "^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$"; Pattern ipPattern = Pattern.compile(ippatternstr); Matcher ipMatch = ipPattern.matcher(ip); String parseError = String.format("Could not parse IP string \"%s\"", ip); int i = 1; if (ipMatch.find()) { try { for (i = 1; i <= 4; i++) { long octet = Long.parseLong(ipMatch.group(i)); if (octet < 0 || octet > 255) { String errMsg = String.format("Octet %s out of range 0-255 in ip %s", ipMatch.group(i), ip); throw new IPOctetOutOfRangeException(errMsg); } out += octet << (8 * -i + 32); } } catch (NumberFormatException e) { String errMsg = String.format("Could not convert %s to integer in ip \"%s\"", ipMatch.group(i), ip); throw new IPStringConversionException(errMsg); } } else { throw new IPStringConversionException(parseError); } return out; }  static String long2ip(long ipLong); static long mask_bits(int bits); static List<String> ipv4BlockToIpStrings(String blockString); static List<String> ipv4BlocksToIpStrings(List<String> ipBlocks); static IPv4Range ipv4BlockToRange(String blockString); static boolean isValid(String ip); static long ip2long(String ip); static boolean rejectUnwantedIps(String ip);  }