[Question]
  [
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:
`0xxxxxxx`
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 [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so shortest code wins.
# Example
Let's take the input 8364 (the euro symbol "€") as an example. We somehow know that we need 3 bytes, so \$n=3\$. Let's take
`xxxxxxxx 10xxxxxx 10xxxxxx 10xxxxxx ...`
And take the first 3 bytes:
`xxxxxxxx 10xxxxxx 10xxxxxx`
Next, the first \$n\$ "x"s are set to 1:
`111xxxxx 10xxxxxx 10xxxxxx`
And then the leftmost "x" is set to 0. (index \$n+1\$ before any "x"s were replaced)
`1110xxxx 10xxxxxx 10xxxxxx`
Finally, we fill the binary expansion of 8364 (which is 10 0000 1010 1100) into the remaining "x"s
`11100010 10000010 10101100`
And convert to bytes:
`[226, 130, 172]`
Now you might wonder how we know what value of \$n\$ to use? One option is trial and error. Start from \$n=1\$ and increment \$n\$ until we find an \$n\$ where the binary expansion of our input fits.
If we had the input 70368744177663 (\$n=9\$) we would start like so:
`xxxxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx`
and then
`11111111 1010xxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx`
and then fill the binary expansion of 70368744177663
# Test cases
```
0 -> [0]
69 -> [69]
127 -> [127]
128 -> [194, 128]
1546 -> [216, 138]
2047 -> [223, 191]
2048 -> [224, 160, 128]
34195 -> [232, 150, 147]
65535 -> [239, 191, 191]
65536 -> [240, 144, 128, 128]
798319 -> [243, 130, 185, 175]
2097151 -> [247, 191, 191, 191]
2097152 -> [248, 136, 128, 128, 128]
30606638 -> [249, 180, 176, 148, 174]
67108863 -> [251, 191, 191, 191, 191]
67108864 -> [252, 132, 128, 128, 128, 128]
20566519621 -> [254, 147, 137, 183, 130, 189, 133]
68719476735 -> [254, 191, 191, 191, 191, 191, 191]
68719476736 -> [255, 129, 128, 128, 128, 128, 128, 128]
1731079735717 -> [255, 153, 140, 140, 153, 136, 166, 165]
2199023255551 -> [255, 159, 191, 191, 191, 191, 191, 191]
2199023255552 -> [255, 160, 160, 128, 128, 128, 128, 128, 128]
64040217759022 -> [255, 174, 163, 186, 134, 155, 164, 180, 174]
70368744177663 -> [255, 175, 191, 191, 191, 191, 191, 191, 191]
70368744177664 -> [255, 176, 144, 128, 128, 128, 128, 128, 128, 128]
34369578119952639221217025744100729453590194597032 -> [255, 191, 191, 191, 191, 165, 184, 145, 129, 139, 182, 177, 159, 176, 167, 155, 139, 159, 138, 163, 170, 143, 151, 141, 156, 154, 134, 183, 176, 175, 170, 178, 168]
```
  
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 47 bytes
```
Ｎθ≔÷⁺³Ｌ↨θ²¦⁵ηＩ⎇‹θ¹²⁸θＥ↨⁺θ⁻Ｘ⁶⁴η×⁴Ｘ³²η⁶⁴⁺ι×⁶⁴⁺²¬κ
```
[Try it online!](https://tio.run/##NU3LbsIwELz3K3JcS6nkZ4zFqYULEkU58AMutYhFcMB2qPr17jq0K@3u7Ghm9jTYeJrsWMou3OZ8mK@fLsKdrF/eUvLnALuQt/7hvxz045xAtM3ehXMe4N0mB/e24YSQtlHYA7r66EOGjU0Zji4GG39g71KqQsZXKELwYW9P95JYCR8Q9NM3vu5kDWqbo7@6BHg8acErvbzqJI7F6v9l1bQwqDpMGS7kr9alCCk6o/SKMWMU74ThnHGmKVdaSkap5kYqoQxluI2mgpfXx/gL "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｎθ
```
Input the integer.
```
≔÷⁺³Ｌ↨θ²¦⁵η
```
Calculate the number of bytes the extended representation would take.
```
Ｉ⎇‹θ¹²⁸θ
```
If the integer is less than 128 then just output the integer again, otherwise...
```
Ｅ↨⁺θ⁻Ｘ⁶⁴η×⁴Ｘ³²η⁶⁴⁺ι×⁶⁴⁺²¬κ
```
... calculate `n-2` high-level bits, add that to the integer, convert to base `64`, then add `192` to the first byte and `128` to the remaining bytes.
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~104~~ 103 bytes
*Saved 1 byte thanks to @emanresuA*
Expects a BigInt. Returns a comma-separated string of bytes.
```
n=>n>127?(g=k=>k>63?g(k>>6n)+[,k&63n|128n]:k|192n)(2n**(x=(h=n=>n?-~h(n/2n):3n)(n)/5n)-4n<<x*5n|n):n+''
```
[Try it online!](https://tio.run/##nZbLbttADEX3@QqtGsnxY57kTBrJq36F4YXhxk6iYFwkRZGF0V93yZlEsoPW9UgLAbLAc8lLDq2n1a/V6/rl8cfPSdh9vz9s6kOom9BIhfNyW7d10zag59uybRoI1c1i3H4BHfZSubC8bffSq1CVKoxG5VtdPtQcPJ/8fijDjF7canoZqpkN1cSEu7u3kQ17@jncXF8fvi6uikKEcZF7zWbFQiwpGHx@NAeD52gqMTucoykuhbth4d6MOTgyrIFcCDOUBGLoyFDC4CCG0sTw8p3hhjG4FhBdQdpIbzNJEaQVMSyDTHQXrNXDQD5W1ZXGIBgCMjGZ1KuuPvROy8ypSzQ2WzPSWbqhTbZ7lFZm4RIN@yKPmsg0NYDG9WnoK@3bKUAA6KzZSEjuguNykblRAU3sB0rhHK2QXKSVn2ruW5yQJh/JQxcn77jwrnolLICVHtTlLUpcE@eY4Xxzfe/ZFq1j0g5pEyBgxpj3cC//eTuFQyach1P5vzly6o1ELQV6yh7lpeunV7DsSDpg4uMxDiDEWzoc0ntBe8HSJfMV/P9dOlZQ2Qpx672vvrNWgRFGKIloSe1CnV4G44Zlg1zc@fyY9E13xOLBQqGp7caQEFx6vI5k7Hm/OtNOZEy2DHxaqmed00aDt@gkNcoq0F4pSUYKZTkBIVB5Q38TXtCwW0@ZqXCk1WU@nU4/nItHMSYRC8ZoHwsCCS6vppvdy7fVmr5eirop1rvwunu@nz7vtuWGPmOq6vAH "JavaScript (Node.js) – Try It Online")
### 92 bytes
An alternate version taking a standard JS number. It only works up to 0x3FFFFFF, which somewhat defeats the purpose of using UTF-∞. :-/
```
n=>n>>7?(g=k=>k>>6?g(k>>6)+[,k&63|128]:k|192)(2**(x=(h=n=>n?-~h(n>>1):3)(n)/5|0)-4<<x*5|n):n
```
[Try it online!](https://tio.run/##XZLLbsIwFET3fEVWlU0N@P2g2Kz6FYgFSnm0QU4FVcUi6q@nvk6aRM0m8py5M9dKPg7fh3t5e//8WsT67diefBt9iCGYLTr7yocqBL09I3jh5x2pnrRoGLf7ddUwxzHi8zl6eHTxMLZd/FxQGmZ4LTCKeKUaihdys3nMVRPxOrYvu1lRUFL0z2pV7Og@SdqRiaQdaIwbMmrp1Il2KjpJQMpESU0Gwlk6MJEJp9JMCBeJONYTOyWQpukQKSRzigxY8EQUYJl30UqJKXY5dwgHrEcs82C379BgnBUs373zwGoCjDYFM6O6JZ1hKuX2HjPWTC4CHj54oAHK/7rGK1FNtRZJ6Iyws4VCA@48Z2Te3jBqrRZ9omL/Wvvq/Wx5qm@vhzJ9@MKHoqzjvb4el9f6jE7pF8C4/QU "JavaScript (Node.js) – Try It Online")
### How?
This is essentially the same method as [@Neil's](https://codegolf.stackexchange.com/a/243467/58563).
Using the recursive function \$h\$, we compute \$x=\left\lfloor(b+3)/5\right\rfloor\$, where \$b\$ is the number of bits in the input.
We compute \$2^x-4\$ (a bit mask consisting of \$x-2\$ ones followed by \$2\$ zeros). We shift it by \$5x\$ positions to the left and OR it with \$n\$.
Using the recursive function \$g\$, we convert the result to base \$64\$ in big-endian order. We add \$192\$ to the leading byte and \$128\$ to the other ones.
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 171 bytes
```
n=>n<128?n:(F='0bxxxxxxxx '.repeat(c=((d=n.toString(2)).length+3)/5|0)[r='replace'](/ 0bxx/g,' 0b10',i=0))[r](/x/g,_=>i++<c?1:d.padStart(5*c+3,0)[i-c])[r](/0b\d{8}/g,eval)
```
[Try it online!](https://tio.run/##VVDbbtswDH3XV@jNUp04pO5q6/RtP9DHtVhd28k8GLLhaMWAtt@e0UtXoAJEkYeHhxR/NS/NqV2GOW/T1PXnQ31O9T7dogp36Vp8qwt4/vNxeFEt/dw3WbS1EF2dqjzd52VIR6GkrMY@HfPPUsudfQP5fakLYo9N2xePYsdXmd1xU5CDUGyGGiRxKLOiP@r9UJa37R1ed9XcdPe5WbKwV22pNyQ1bNvHCxmeH7rX8E4l/UszynM7pVPmQ5p/Z17zJ2AuMlSebmBojWMKjF9NYNpgtMxZqy/WMR@DxkjZ6NHix6uYBgfO6cCcRwjB6f@OIYp1zmJ0CpkLHqPxzq96n75j6DWCjwR7pNYYIyitLB38EinmDBhQ6L0lUDEPmnSMIYDafw0Nja9dtD4gSVjldFQKqRaUXTkAXkVDX4tAg9hIxeqpOs3jkEXxkAp5ww7TwsXnvvh0uKxN8hWbxr4ap6Mo11TJC77dkyn54R8i5c35Lw "JavaScript (Node.js) – Try It Online")
Since `n<128` is an edgecase, just handle it there and then.
Next, the code determines the number of bytes using a simple formula: if there are `c` bytes, then there are `5c+1` x's, so we use that. This means subtracting one from the number of bits in the binary representation and dividing that by five. We repeat `xxxxxxxx`  that many times. Replace the first two x's of any byte that is not the first with a `10`, then replace the remaining x's with the (padded) binary representation of `n`, then evaluate each byte.
Output format: as a number for `n<128`, as a space-separated string (with trailing newline) otherwise. Let me know if this is not allowed.
Fails on last testcase due to JavaScript's limitations.
*Thanks @Neil for -7 bytes.*
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 137 bytes
```
n=>n<128?[n]:eval(`[0b${('1'.repeat((L=(s=n.toString(2)).length+3)/5)+'0'.repeat(~(4.8*~L%6-2))+s).match(/^.{8}|.{6}/g).join`n,0b10`}n]`)
```
[Try it online!](https://tio.run/##hVRNb9pAEL3zK3xo5XUTnP3@aGoqVeqhUm49UipISggRWSOwokiU/HUyM14TN01TDsvuzLw3b2fGezu7n22vNst1M4z1r/nhujrEahQ/Cek/j@Pk4/x@tmLTMb98t2O5yMvNfD2fNYxdVGxbxbKpvzebZVwwWRTlah4Xzc2JKs5McZLzY/Aj06X/8Hjx3g4h7GRblHez5uqGnf0sd37/u9zZ/dmiKG/rZZzGU34p@HQfJ9Pi0GRVNh3wbDjKxnwysIF2NkwGQjrawz8efHsI@jSDA1iMtmSSwoJJgUly3UKkVGAKgkw@mRBoeUIrLYJpHUqCzaBDQyJrjOocgUgSEzpSQk3BrZDE54JXIiQ3JlcY4w0szqCM4IQRye@eeY8y0S@TH0mVfabvNHPLrVXpPhrVecziMJRAToNOJ7j3VrVRRrzI1d2mjdIpCktAdehnTGklN9YaEaxM@o2mWiEEF/98XZSkFLB7B41y1nWlJEgQ/1z6kFRkg7WT4TVNfXXCKcFdgExOuB7SoKq2Ubw7Uk0tLdgTEQKH7hv4iT4y/E9rHyl7SJquNGJvSLaaay6FcwZY@nhHI4pKPY00Hltifew1dNhxBdXSGhjssc/tpL2tPMn/A6/7ePtirt@4hNLKBuO8gFoYaVWQUsCduDTIzLmTQcOXFDh01QRI2b/oa8osOjyN1rHz9AV6nEvnus6QSOu60lAIOZTvqueo7bij8de4GITRGFJZaWiJiopGCEcEcLdpCQ/eHSvK7Xq1bFj@I@b4nq3ZQ1aNsofOPBzlRXE@aMrrevN1Bo9dRPdukGVXddzWq3m5qhfsmn1ZLr7FhkV43@BpzPOsqjJ6c@NYTNBwmv0dBMT74vzwBA "JavaScript (Node.js) – Try It Online")
StackExchange code highlight is broken again... Not sure if using string based idea could golf some more bytes or not.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 33 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
žy@ibg3+5÷U64Xm32Xm4*-+64вāΘÌ64*+
```
Port of [*@Neil*'s Charcoal answer](https://codegolf.stackexchange.com/a/243467/52210).
[Try it online](https://tio.run/##yy9OTMpM/f//6L5Kh8ykdGNt08PbQ81MInKNjSJyTbR0tc1MLmw60nhuxuEeMxMt7f//zQ2MzSzMTUwMzc3NzIwB) or [verify all test cases](https://tio.run/##VU@7SkNhDH6V0rEnQu75M7UvIRTEwYJIh@IgCGfo4OLkIJ18Dhc3l6Or@Ay@yDEHL9BAbl@SL8n1zcVmezne9sv57Ov@MJsv@@FxfH/tV9vNlXQ2vJy6rnfC650uTjrXz@e3u4@n4cF10Y17GM8QPIE4ShuQqQOjxmQaiFIauJn8WIfIJpRVzSCjX88g6OguDTwIW3P5C7RazN0onQm8BaWGx8T3HztQCGFkwUG1mjKRha2EjjIGV1RkirACGQKleFQLqPXHqdb54mnRqCiMXZKZahbZph7E4NR6LbEOsaxhPv8G).
**Explanation:**
In pseudo-code:
For inputs \$<128\$, simply output the input  
For inputs \$\geq128\$:
1. With \$n\$ as input, first calculate: \$x=\left\lfloor\frac{len(bin(n))+3}{5}\right\rfloor\$
2. Then calculate \$y=64^x-4\times32^x+n\$
3. Convert this \$y\$ to a base-64 list
4. Add 128 to each item, and an additional 64 to the very first item, which is the intended output
```
žy@i             # If the (implicit) input-integer is >= 128:
    b            #  Convert the (implicit) input to a binary string
     g           #  Pop and push its length
      3+         #  Add 3
        5÷       #  Integer-divide that by 5
          U      #  Pop and store it in variable `X`
    64Xm         #  Push 64 to the power `X`
        32Xm     #  Push 32 to the power `X`
            4*   #  Multiply it by 4
              -  #  Subtract it from each other
               + #  Add the (implicit) input to it
    64в          #  Convert this integer to a base-64 list
       ā         #  Push a list in the range [1,length] (without popping)
        Θ        #  Convert every non-1 to 0 (with an ==1 check)
         Ì       #  Add 2 to each
          64*    #  Multiply each by 64
             +   #  Add the values at the same positions in the lists together
                 #  (after which this list is output implicitly as result)
                 # (implicit else:)
                 #  (implicitly output the implicit input)
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 101 bytes
```
n=>(h=x=>x>>8n?[...h(x>>6n),x%64n+128n]:[x])((g=x=>n>>7n?n>>1n+5n*x?g(++x):64n**x-32n**x:x)(0n)*4n+n)
```
[Try it online!](https://tio.run/##nZbPbhpBDMbveYq9VNoFQsbzx56hYjn1KRAHRAO0ioYqqap9e2rPJLsQtQmzHAZWwr/P/uwx/Nz@2b7snn/8@n0fT98fz/vlOS7b@rjslm3Xtj6u1vP5/FjzZ4zNrPuCNk5B@7hZrLtNU9cH@WZsW4orPiFOXZx0q0M9nXbNgr88mXT3RsvbomtqFZsJA2Jz/rq@qyoVZ1Xp6@GhWqsNB2Moj5ZgDBINmorDJZrjcrgfFx7sTIITw1kshQhDAzLDJIZWlkYxtGFGgFeGH8eQWlD1BRkLwRWSEshoZjgB2eQuOmfGgUKqqi9NQDgGZFMyuVd9fRS8gcKpyzQx2wjSOz7IZdsDgYMiXKbRUORFE4WmR9CkPoNDpUM7FSpEUzQbGSld8FIuCTcpkE39IFDeoylGOnhX89DijLTlSBm6NHmXhffVa@UQHQTUt7coc22aY4HL4Yfeiy3GpKQ98SYgpIIxH@AB/ntcw7EQLsOpw78cufYGyICiwNkT3Lp@BgUnjuQLpt4e0wBiOvLlgBAU7wXHLyhXCJ@7dKmgixXS1ntdfR9ahVZZpYHIsdqNOoMMpQ0rBvm08@Ux69v@iqWLRcpw261lIbz1el3IuI/96k27krHFMvhuqX7onLEGgyMP3Cin0QStgY1U2kkCSpEOln8mguJhd4Ez0/FCq8@c/768OZeuYkoiFUzJPhFEFtzczfen52/b3bGO1bKtdqf4cnp6nD@dDvW@jk3TnP8C "JavaScript (Node.js) – Try It Online")
]