Skip to main content
1 of 3
AnttiP
  • 7.9k
  • 8
  • 10

UTF-∞

It's 22022 and the Unicode consortium is having a problem. After the writing system of the ⮧⣝Ⅲⴄ⟢⧩⋓⣠ civilization was assigned the last Unicode block, the consortium members have been scrambling to find a new encoding to replace UTF-8. Finally UTF-∞, a proposal by Bob Rike, was adopted. UTF-∞ is backwards compatible with UTF-8. If you know how UTF-8 works, then TLDR; UTF-∞ is the natural extension of UTF-8.

UTF-∞, like UTF-8, encodes an integer to some sequence of bytes like so (each byte shown as 8 bits)

xxxxxxxx 10xxxxxx 10xxxxxx 10xxxxxx ...

If the sequence of bytes has length \$n\$, then the first \$n\$ x:s (from left to right), are set to 1 and the \$n+1\$:th x is set to 0. The rest of the x:s encode a big-endian binary representation of the integer.

There is an exception. If the length of the sequence is 1 (meaning the input number is less than 128), then the encoding looks as follows:

0xxxxxxxx

Where the x:s contain the binary representation of the integer.

Also, in order for an encoding to be valid, the minimum amount of bytes has to be used (no overlong encodings).

Your task is to take in a non-negative integer and output the UTF-∞ representation of the integer. You can output a list/string of bytes or a list of numbers between 0 and 255 inclusive. This is so shortest code wins.

AnttiP
  • 7.9k
  • 8
  • 10