How to compress an IPv6 address, and expand it back

How to compress an IPv6 address, and expand it back

2001:0db8:0000:0000:0000:ff00:0042:8329 and 2001:db8::ff00:42:8329 are the same address. Two rules shorten it, one rule limits where :: can go, and some systems still need the long form back.

One address, many spellings

An IPv6 address is 128 bits, written as eight groups of four hex digits. The same address can be written with or without leading zeros, in upper or lower case, and with or without ::. Searching logs, comparing firewall rules, or matching DNS records as text fails when two systems spell the address differently.

RFC 5952 fixes one form for each address, the one to use in documentation and logs. Some systems want the opposite: reverse DNS zones and some firewalls need all 32 hex digits.

Compress, expand, and find the block

The IPv6 Compress / Expand tool takes one address or a list, one per line. It reads each address into its 128 bits first, so any valid spelling gives the same result, then writes it in RFC 5952 form or in full. Invalid lines are marked and explained. A prefix such as /64 or a zone index such as %eth0 makes a line invalid, so remove those first.

For an address with a prefix, the IPv6 CIDR Calculator shows the network, the first and last address, and the exact count, in full and compressed form. 2001:db8::/64 holds 18,446,744,073,709,551,616 addresses, and a /48 holds 65,536 /64 subnets.

Compress an IPv6 address

  1. Write the address as eight groups of four hex digits, for example 2001:0db8:0000:0000:0000:ff00:0042:8329.
  2. Drop the leading zeros in each group: 0db8 becomes db8 and 0042 becomes 42. A group of all zeros becomes 0.
  3. Find the longest run of two or more 0 groups and replace it with ::. If two runs are the same length, shorten the first one.
  4. Leave a single 0 group as 0. It is never replaced with ::.
  5. Write any hex letters in lowercase. The example becomes 2001:db8::ff00:42:8329.
  6. Check the result, or a whole list, in the IPv6 Compress / Expand tool.

Where the compression rules trip people up

  • :: can appear only once. An address with two is invalid, because nobody could tell how many zero groups each one stands for.
  • Addresses with a dotted IPv4 tail are accepted, but the output is written fully in hex.
  • The CIDR calculator shows the block an address belongs to, not whether that block is assigned or routed.

Frequently asked questions

What are the rules for shortening an IPv6 address?

Lowercase hex, no leading zeros in a group, and the longest run of two or more zero groups written as ::.

Can :: be used twice in an IPv6 address?

No. Two would make the address ambiguous, so it is invalid.

How do I expand ::1?

Write all eight groups with four digits: 0000:0000:0000:0000:0000:0000:0000:0001.

How is 2001:db8:0:0:1:0:0:1 compressed?

As 2001:db8::1:0:0:1. The two zero runs are the same length, so the first one is shortened.

Tools used in this guide

More from the blog

All guides