CIDR to IP Range Converter & IP Range to CIDR (IPv4/IPv6)
CIDR to IP Range Converter (and IP Range to CIDR)
Convert CIDR blocks to first/last IP and address count, convert any IP range to the minimal list of CIDR prefixes, merge (aggregate) prefix lists and exclude a prefix. IPv4 and IPv6, exact 128-bit math, one entry per line. Everything runs in your browser.
Examples: 10.0.0.0/8, 192.168.1.77/26, 172.16.0.0/255.240.0.0, 2001:db8::/48. A single address counts as /32 or /128. Text after # is a comment.
Calculating…
Plain list
CSV output
What this tool does
This page is a CIDR to range converter and an IP range to CIDR converter in one tool, for IPv4 and IPv6. It has three modes:
- CIDR → IP range: paste prefixes such as
10.20.0.0/22or2001:db8::/48, one per line. For each line you get the network, the first IP, the last IP, the exact number of addresses (128-bit math, no rounding), and the netmask and wildcard mask for IPv4. Netmask input like172.16.0.0/255.240.0.0is accepted too. - IP range → CIDR: paste ranges as
start - endorstart end. Each range is split into the smallest possible list of CIDR blocks, which is what you need for ACLs, firewall objects, prefix lists and route filters that only accept prefixes. - Merge / exclude: paste a list of prefixes and get the collapsed minimal list (route aggregation / summarization), optionally minus one or more excluded prefixes, for example a /22 with one /25 carved out.
Every input line is validated separately and errors name the line number. Results can be copied as a plain list (one entry per line) or as CSV for a spreadsheet, and the URL hash keeps your input so you can bookmark or share a calculation.
How it works
CIDR to range
An address has B bits (32 for IPv4, 128 for IPv6). A prefix of length p covers 2B−p addresses (RFC 4632 section 3.1 for IPv4, RFC 4291 section 2.3 for IPv6 prefixes):
mask = (2^B - 1) XOR (2^(B-p) - 1)first IP = address AND masklast IP = first IP OR (NOT mask)= first + 2B−p − 1addresses = 2^(B-p)
The calculator uses JavaScript BigInt, so an IPv6 /0 gives exactly 340282366920938463463374607431768211456 addresses. The first and last IP are the full block; for IPv4 subnets the usable host range is usually first+1 to last−1 (except /31 and /32), which the IP subnet calculator shows in detail. If a line has host bits set (for example 10.0.0.77/22), the tool reports the containing network 10.0.0.0/22 and adds a note. A dotted mask after the slash is read as a netmask; a mask that is only valid as a wildcard (for example 0.0.0.255) is read as a host mask, the same rule Python's ipaddress uses.
IP range to CIDR
The range is cut greedily from the start address. At each step the block size is the largest power of two that (a) the current start address is aligned to and (b) still fits in what is left of the range:
n = min(trailing_zero_bits(start), floor(log2(end - start + 1)))- emit
start/(B - n), thenstart = start + 2^n, repeat whilestart <= end
This is exactly the algorithm of Python's ipaddress.summarize_address_range(), and it always yields the minimal number of CIDR blocks for a range. A range in one line must stay inside one address family: an IPv4 start with an IPv6 end is rejected.
Merge (aggregate) and exclude
Merging converts every prefix to an interval [first, last], sorts the intervals, joins overlapping or adjacent ones, and runs the range-to-CIDR algorithm on every joined interval. The result equals Python's ipaddress.collapse_addresses(): the minimal set of prefixes covering exactly the same addresses. IPv4 and IPv6 are merged separately (IPv4 listed first). Excluding prefix X from block Y works like address_exclude(): Y is split in two halves, the half that does not contain X is kept, and the other half is split again until X itself is reached. That produces (Y length − X length) blocks. When several prefixes are excluded, the result is merged again and sorted.
Address text format
IPv6 output follows the canonical RFC 5952 format (lower case, longest run of zero groups shortened to ::, the first one on a tie). IPv4-mapped addresses (::ffff:0:0/96) are printed with the IPv4 part in dotted decimal, as RFC 5952 section 5 recommends and as Python 3.13+ prints them. IPv4 octets with leading zeros (010.1.1.1) are rejected because some systems read them as octal.
Sources: RFC 4632 (CIDR), RFC 4291 (IPv6 addressing architecture), RFC 5952 (IPv6 text representation), and the Python ipaddress documentation and source (summarize_address_range, collapse_addresses, address_exclude), which the test suite of this tool uses to generate its expected results.
Worked examples
- 192.168.10.0/24 to range. 232−24 = 256 addresses, first IP 192.168.10.0, last IP 192.168.10.255, netmask 255.255.255.0, wildcard 0.0.0.255.
- Host bits set: 10.0.0.77/22. The mask 255.255.252.0 clears the last 10 bits, so the network is 10.0.0.0/22, range 10.0.0.0 to 10.0.3.255, 1,024 addresses.
- IPv6 2001:db8:abcd::/48. First IP 2001:db8:abcd::, last IP 2001:db8:abcd:ffff:ffff:ffff:ffff:ffff, 280 = 1,208,925,819,614,629,174,706,176 addresses.
- IP range to CIDR: 192.0.2.0 - 192.0.2.130. 192.0.2.0 is aligned to /25 and 128 addresses fit, so the first block is 192.0.2.0/25. Three addresses remain (128 to 130): 192.0.2.128/31 and 192.0.2.130/32. Result: 3 prefixes.
- A messy range: 192.168.1.10 - 192.168.1.200 needs 8 prefixes: 192.168.1.10/31, 192.168.1.12/30, 192.168.1.16/28, 192.168.1.32/27, 192.168.1.64/26, 192.168.1.128/26, 192.168.1.192/29, 192.168.1.200/32.
- Merge and exclude. 10.0.0.0/24, 10.0.1.0/24 and 10.0.2.0/23 merge into 10.0.0.0/22. Excluding 10.0.2.128/25 from it leaves 10.0.0.0/23, 10.0.2.0/25 and 10.0.3.0/24 (22 → 25 = 3 blocks). In the same way 192.0.2.0/28 minus 192.0.2.1/32 gives 192.0.2.0/32, 192.0.2.2/31, 192.0.2.4/30 and 192.0.2.8/29.
Prefix length reference
| IPv4 prefix | Netmask | Addresses | IPv6 prefix | Addresses |
|---|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,216 | /32 | 296 = 79,228,162,514,264,337,593,543,950,336 |
| /12 | 255.240.0.0 | 1,048,576 | /48 | 280 = 1,208,925,819,614,629,174,706,176 |
| /16 | 255.255.0.0 | 65,536 | /56 | 272 = 4,722,366,482,869,645,213,696 |
| /20 | 255.255.240.0 | 4,096 | /60 | 268 = 295,147,905,179,352,825,856 |
| /22 | 255.255.252.0 | 1,024 | /64 | 264 = 18,446,744,073,709,551,616 |
| /24 | 255.255.255.0 | 256 | /96 | 232 = 4,294,967,296 |
| /26 | 255.255.255.192 | 64 | /112 | 216 = 65,536 |
| /28 | 255.255.255.240 | 16 | /120 | 256 |
| /30 | 255.255.255.252 | 4 | /126 | 4 |
| /31 | 255.255.255.254 | 2 | /127 | 2 |
| /32 | 255.255.255.255 | 1 | /128 | 1 |
FAQ
How do I convert a CIDR block to an IP range?
AND the address with the netmask to get the first IP, then add 2^(32 - prefix length) - 1 for IPv4 or 2^(128 - prefix length) - 1 for IPv6 to get the last IP. For example 10.0.0.0/22 covers 10.0.0.0 to 10.0.3.255, which is 1024 addresses.
How do I convert an IP range to CIDR notation?
Start at the first address and repeatedly take the largest block that the current address is aligned to and that still fits before the end address. For example 192.0.2.0 - 192.0.2.130 becomes 192.0.2.0/25, 192.0.2.128/31 and 192.0.2.130/32. This is the same algorithm as Python ipaddress.summarize_address_range.
Why does one IP range turn into several CIDR blocks?
A CIDR block always has a power-of-two size and starts at an address that is a multiple of that size. A range that does not start and end on such boundaries cannot be one prefix, so it is split into the minimal number of aligned blocks. An IPv4 range can need up to 62 prefixes and an IPv6 range up to 254.
What happens if my CIDR has host bits set, like 192.168.1.77/26?
The tool clears the host bits and shows the containing network, here 192.168.1.64/26 with the range 192.168.1.64 to 192.168.1.127, and adds a note so you can fix the source list. In merge mode the same normalization is applied before aggregation.
Can I mix IPv4 and IPv6 in the same list?
Yes, every line is handled on its own and merged lists keep IPv4 and IPv6 separate. Only a single range line cannot mix families: an IPv4 start address with an IPv6 end address is rejected with an error for that line.
How many addresses are in an IPv6 /64 or /48?
A /64 has 2^64 = 18446744073709551616 addresses and a /48 has 2^80 = 1208925819614629174706176 addresses. The tool computes these counts exactly with 128-bit integer math instead of rounded floating-point numbers.
What is the difference between merging and excluding prefixes?
Merging (aggregation) replaces overlapping and adjacent prefixes with the smallest list that covers exactly the same addresses, for example 10.0.0.0/24 and 10.0.1.0/24 become 10.0.0.0/23. Excluding removes a prefix from a larger block and returns the remaining pieces, for example 10.0.0.0/23 minus 10.0.1.0/24 leaves 10.0.0.0/24.
Comments
Post a Comment