How to compress an IPv6 address
By Flavio
Compress an IPv6 address to the RFC 5952 text form in the browser. You also get the expanded form, and a zone id is kept on both lines.
Free toolThis tutorial uses the IPv6 Address Formatter. It runs in your browser, so open it in another tab and follow along.
An IPv6 address is 128 bits. The text form writes those bits as eight groups of four hex digits, with colons between the groups. The full form is long, and people shorten it. Two people shortening the same address can get two different strings, and a comparison that uses the strings then fails.
RFC 5952 picks one shortened form. Hex is lowercase. Leading zeros inside a group are dropped. The longest run of zero groups, if it is at least two groups, becomes ::. If two runs tie, the one on the left wins. A single 0 stays 0. The IPv6 address formatter prints that form, and it also prints the expanded form so you can see all eight groups.
The two lines
The expanded line is the address with nothing hidden. Each group is four lowercase hex digits. 2001:db8::1 expands to 2001:0db8:0000:0000:0000:0000:0000:0001. You use this line when a log, a filter, or a comparison wants a fixed width.
The compressed line is the RFC 5952 recommendation. Same bits, shorter text. :: appears once. It stands for the groups that were squeezed out, and you can count them: eight minus the groups you can still see.
A zone id is the text after the first %. RFC 4007 uses it to say which interface a link-local address belongs to. It is not part of the 128 bits. The page copies it onto both lines. fe80::1%eth0 stays scoped to eth0 after compression.
An IPv4-mapped address is the prefix ::ffff:0:0/96. The last 32 bits are an IPv4 address. RFC 5952 says the recommended text for that prefix keeps a dotted tail: ::ffff:192.0.2.1. Other addresses that happen to be written with a dotted tail are compressed to hex.
Compress the sample
Open the IPv6 address formatter and click Load sample. The sample is the documentation prefix from RFC 3849, written the long way, with capital hex:
2001:0DB8:0000:0000:0000:0000:0000:0001
The page prints:
Expanded: 2001:0db8:0000:0000:0000:0000:0000:0001
Compressed: 2001:db8::1
The note is: Hex digits are lowercase in the RFC 5952 form. DB8 became db8 on both lines. Six zero groups in the middle are the longest run, and the only run, so they become ::. The first group keeps 2001. The second group drops its leading zero and is db8. The last group drops three leading zeros and is 1.
2001:db8::1 and the long form are the same 128 bits. A string compare of the two texts fails. Compare the compressed lines, or compare the expanded lines.
Count the groups hidden by :: before you trust a shortened address from a ticket. 2001:db8::1 shows two groups, so :: stands for six. 2001:db8::1:0:0:1 shows six groups: 2001, db8, 1, 0, 0, and 1. The double colon stands for the other two. That is why 2001:db8:0:0:1:0:0:1 compresses to 2001:db8::1:0:0:1. The later pair of zeros stays written out. The two runs have the same length, and the left one wins.
Two zero runs, and a single zero
This address has a run of two zeros and a run of three:
2001:0:0:1:0:0:0:1
The page prints:
Expanded: 2001:0000:0000:0001:0000:0000:0000:0001
Compressed: 2001:0:0:1::1
The run of three is longer, so it is the one that becomes ::. The run of two on the left stays as 0:0. If the runs were the same length, the left one would win. 2001:db8:0:0:1:0:0:1 compresses to 2001:db8::1:0:0:1, because the first run is the leftmost of two equal runs of length 2. The later 0:0 stays written out.
A single zero is never compressed. 2001:db8:0:1:1:1:1:1 stays 2001:db8:0:1:1:1:1:1. The note is: A single 0 was left as 0. RFC 5952 does not compress one zero group. :: would be wrong there. It would hide one group, and the rule wants two or more.
:: by itself is all eight groups zero. The expanded line is 0000:0000:0000:0000:0000:0000:0000:0000. ::1 is the loopback address. Seven zeros, then 1.
Mapped addresses and zone ids
::ffff:c000:201 and ::ffff:192.0.2.1 are the same mapped address. The hex groups c000 and 0201 are the bytes 192, 0, 2, and 1. The compressed line for both inputs is:
Expanded: 0000:0000:0000:0000:0000:ffff:c000:0201
Compressed: ::ffff:192.0.2.1
The note is: The compressed form uses a dotted IPv4 tail because this is an IPv4-mapped address (::ffff:0:0/96). The expanded line stays in hex, so you can still see the 16-bit groups. Only the compressed line uses dots.
::192.0.2.1 looks similar and is not mapped. The ffff group is missing. Its compressed form is ::c000:201. The dotted tail was only a way to write two groups. It is not kept, because this address is not in ::ffff:0:0/96.
A link-local address with a zone:
FE80::1%eth0
becomes:
Expanded: fe80:0000:0000:0000:0000:0000:0000:0001%eth0
Compressed: fe80::1%eth0
Two notes. The zone id is a scope, not part of the 128-bit address. Hex digits are lowercase in the RFC 5952 form. FE80 is fe80 on both lines, and %eth0 is copied as you typed it. The page does not lowercase the zone, and it does not check that eth0 exists.
What goes wrong
2001:db8::1::2 has two ::. The error is: An IPv6 address can contain :: only once. The same message is used for :::, which is three colons. You cannot tell how many groups each :: replaced, so the address is rejected instead of guessed.
1:2:3:4:5:6:7 has seven groups and no ::. The error is: This address has 7 groups. IPv6 needs 8. Add the missing group, or use :: if the missing part is a run of zeros.
192.0.2.1 is IPv4. The error is: This is an IPv4 address. The page formats IPv6 text. A prefix such as 2001:db8::/32 is not an address either. The slash is not part of the text form this page reads. The CIDR calculator is the page for a prefix.
::ffff:192.0.2.01 fails with: “01” is not a decimal octet. A decimal octet is 0 or a number that does not start with zero. 192.0.2.1 is fine. 192.0.2.01 is not, because a leading zero used to mean octal in some parsers, and the safe rule is to reject it.
gggg::1 fails with: “gggg” is not a hex group. A group is at most four hex digits. fe80::1% fails with: The zone id is empty. A second % fails with: The zone id contains %.
An empty box says: Paste an IPv6 address. More than 200 characters is refused. The address you type is stored in the page URL as ?addr=, because it is short. A certificate or a log is not stored that way on the other tools. Do not put a secret in this box. An address is not usually a secret, and a zone name on your laptop is still a name.
When the page is the wrong tool
Use this page when you have one address and you want the RFC 5952 string, or you want to see the eight groups written out. Comparing two logs, writing a fixture, and checking a firewall rule are the usual reasons.
Use the CIDR calculator when you have a prefix length and you want the network and the size. 2001:db8::/32 belongs there. This page will reject the slash. Use a DNS tool when you have a name and you want the address a resolver returns. The formatter does not query anything. It only rewrites text you already have.
Secure a VPS uses 2001:db8::10 as a stand-in for a server address. That string is already compressed. Paste it here if you want the expanded groups before you put the real address in an SSH config or a firewall rule. The Node.js os module prints ::1 and fe80::1 from networkInterfaces(). Those strings are the compressed form. The expanded form of fe80::1 is fe80:0000:0000:0000:0000:0000:0000:0001.
Try the IPv6 Address Formatter
Paste an IPv6 address and see the expanded form and the RFC 5952 compressed form. A zone id is kept. The address can be stored in the URL.
Open the IPv6 Address Formatter →
Related tools
- CIDR Calculator: IPv4 CIDR and netmask calculator with network/host breakdown, binary view, and a prefix finder for N hosts.
- DNS Record Explainer: DNS record reference (A, MX, TXT, and more) plus a helper for pointing domains, email setup, and anti-spoofing.
- HTTP Response Headers Explainer: Paste response headers and get each one explained — plus warnings for missing security headers like HSTS and CSP.
Want me to talk about your product? You can sponsor this site.