Everyone should know IPv6 by now. IPv6 has very long, cumbersome addresses. In 1996 some very intelligent people created a scheme to encode them much better in RFC 1924. For reasons I cannot understand, this hasn't been widely adopted. To help adoption, it is your task to implement a converter in the language of your choice. Because storage is very expensive, especially in source code repositories, you obviously have to use as little of it as possible.


Write an encoder for the address format specified in RFC1924. The input is a IPv6 address in the boring standard format.

Stolen from Wikipedia this is:

An IPv6 address is represented as eight groups of four hexadecimal digits, each group representing 16 bits (two octets). The groups are separated by colons (:). An example of an IPv6 address is:


The hexadecimal digits are case-insensitive, but IETF recommendations suggest the use of lower case letters. The full representation of eight 4-digit groups may be simplified by several techniques, eliminating parts of the representation.

Leading zeroes

Leading zeroes in a group may be omitted. Thus, the example address may be written as:


Groups of zeroes

One or more consecutive groups of zero value may be replaced with a single empty group using two consecutive colons (::). Thus, the example address can be further simplified:


The localhost (loopback) address, 0:0:0:0:0:0:0:1, and the IPv6 unspecified address, 0:0:0:0:0:0:0:0, are reduced to ::1 and ::, respectively. This two-colon replacement may only be applied once in an address, because multiple occurrences would create an ambiguous representation.

You must handle omitted zeroes in the representation correctly!

The output is a IPv6 address in the superior RFC1924 format.

As in the RFC this is:

The new standard way of writing IPv6 addresses is to treat them as a 128 bit integer, encode that in base 85 notation, then encode that using 85 ASCII characters. The character set to encode the 85 base85 digits, is defined to be, in ascending order:

         '0'..'9', 'A'..'Z', 'a'..'z', '!', '#', '$', '%', '&', '(',
         ')', '*', '+', '-', ';', '<', '=', '>', '?', '@', '^', '_',
         '`', '{', '|', '}', and '~'.

The conversion process is a simple one of division, taking the remainders at each step, and dividing the quotient again, then reading up the page, as is done for any other base conversion.

You can use the suggested conversion algorithm in the RFC or any other you can think of. It just has to result in the correct output!

Example Input and Output

Here is the one from the RFC:


1080:0:0:0:8:800:200C:417A or 1080::8:800:200C:417A



Your program can take input and return the output in any standard way. So it is ok to read from stdin and output to stdout or you can implement your code as a function that takes the input as argument and returns the output.

Winning criteria

As always standard loopholes are disallowed

This is so shortest correct code in bytes wins!

This is my first challenge, so please do notify me about any problems/ambiguities in the description.

  • Is inet_pton allowed? – kennytm Jan 27 '16 at 11:52
  • Functions from the standard library of your language are allowed. I guess that qualifies, so yes it is allowed. – Josef Jan 27 '16 at 14:56
  • Related – Digital Trauma Jan 27 '16 at 16:11
  • Out of curiousity, is For reasons I cannot understand intended to be sarcastic or genuine? – Not that Charles Jan 27 '16 at 20:53
  • @NotthatCharles it is intended to be sarcastic. Most people will understand why that's not widely used after seeing one address in that format and I am one of them. I also understand why IPoAC is not widely used, but I would like it to be. In general, I just think the first of April RFCs are great. – Josef Jan 28 '16 at 7:32

Pyth, 93 91 bytes

Ks[jkUTrG1G"!#$%&()*+-;<=>?@^_`{|}~")$from ipaddress import*;z=int(ip_address(z))$sm@Kdjz85

Second submission in Pyth!

Can't try online because $ is unsafe.

When tested against the example, produces the expected result:

$ pyth -c 'Ks[jkUTrG1G"!#$%&()*+-;<=>?@^_`{|}~")$from ipaddress import *$$z=int(ip_address(z))$sm@Kdjz85'


  • Saved two bytes by replacing $$ with ; and removing space before *


First part creates the mapping string and assigns it to K:


This is done as follows:

K                                       implicitly assign to K
 s[                                 )   the string joined from the following list
     UT                                 range [0..9]
   jk                                   joined with the empty string: 0123456789
       rG1                              capitalized version of the string G (whole alphabet)
          G                             G itself
           "!#$%&()*+-;<=>?@^_`{|}~"    all other symbols (shorter to declare than generate)

Then, the second part is two pieces of normal python amounting to 47 bytes, to convert the input ipv6 address to a number (because z is accessed later in the code, it is first implicitly assigned to any input value before running the Python code)

*from ipaddress import *$$z=int(ip_address(z))$

Then, the last part converts the resulting number to base 85 as a list and maps every value to the mapping string K:

     jz85  convert z to base 85
 m         map over the converted number with lambda d:
  @Kd      K[d]
s          join as a string and implicitly print

Pure Pyth, 100 bytes


You can try it here

In order to support leading/middle/trailing ::, I must insert a list of missing zeroes in the correct position in the ip once converted to a list of hex numbers, which takes up more space than the inline Python version. There's room for improvement but I doubt it can beat the other version!

63 bytes version

Doesn't support the :: notation to skip zeroes:


You can try it here

The different part here is converting the ipv6 to a number using base conversions:

         z          read string from input and assign it to z
        c \:        split it at columns
   m                map over the split list with lambda d:
    id16            convert d from base 16 to 10
  i         65536   convert list of ints from base 65536 to 10
=z                  assign result back to z

GCC C (functions) on x86, 240 bytes

This uses a couple of GCC specials:

#define U unsigned __int128
#define b(n) y[n&1]=__builtin_bswap64(y[n]);
g(U x){x?g(x/85),x%=85,putchar(x>9?x<36?x+55:x<62?x+61:"!#$%&()*+-;<=>?@^_`{|}~"[x-62]:x+48):0;}
f(char *a){long long y[3];inet_pton(10,a,&y[1]);b(2);b(1);g(*(U *)y);}


  • f() is the entry point. It takes a single char *
  • inet_ntop() converts full or compressed string representation of input address to the elements 1 and 2 of a 64-bit integer array
  • Output from inet_ntop() is in network order (big endian), but the integer is needed in host order (little endian on x86) in order to perform arithmetic required for base conversion. Unfortunately gcc does not provide a __builtin_bswap128(). Instead the element 2 of the 64-bit integer array is bswap64()ed to the element 0 and the element 1 is bswap64()ed to itself. Elements 0 and 1 of the array now contain the address correctly in a host order 128-bit integer.
  • Standard successive div/mod by 85 is used to present the integer in base 85. Because the first div/mod yields the last base 85 digit (and so on), the function g() performs this conversion recursively. The digits are effectively pushed to the stack and then output in the correct order.
  • Conversion of digit value to ASCII character is a little messy. This is probably golfable.

Test driver

Mildly ungolfed for readability.

Compile with make ipv6enc or gcc ipv6enc.c -o ipv6enc:

#define U unsigned __int128
#define b(n) y[n&1]=__builtin_bswap64(y[n]);
g(U x){
f(char *a){
  long long y[3];
  g(*(U *)y);

int main (int argc, char **argv) {


    return 0;

To do - Big endian version, 165 bytes

Find a big-endian platform where I can use the gcc 128-bit integers, so I can get rid of all the byte swapping stuff. The code should look like this, but right now this is untested on a big-endian platform:

#define U unsigned __int128
g(U x){x?g(x/85),x%=85,putchar(x>9?x<36?x+55:x<62?x+61:"!#$%&()*+-;<=>?@^_`{|}~"[x-62]:x+48):0;}
f(char *a){U y;inet_pton(10,a,&y);g(y);}
  • 1
    For the big-endian version, my first attempt was to run raspbian under qemu. Unfortunately compilation yields error: ‘__int128’ is not supported for this target. Perhaps GCC will only do int128 on 64-bit targets? Perhaps this next? – Digital Trauma Jan 28 '16 at 0:45

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.