[Question]
  [
Given a string and the characters used to encode it, you need to compress the string by only using as many bits as each character needs. You will return the character codes for each character needed to create a compressed string.
For example, given the string `"the fox"` and the encoder characters `" abcdefghijklmnopqrstuvwxyz"`, the output should be `[170, 76, 19, 195, 32]`.
## How, though?
First, you need to map each encoder character to some bits. If we have the encoder characters `abc`, then we can map the characters to bits, by mapping the character to the position of the character in binary, like this:
```
a => 01
b => 10
c => 11
```
With `13579`, we would map it like this:
```
1 => 001
3 => 010
5 => 011
7 => 100
9 => 101
```
Note that we pad zeros at the beginning as many as necessary.
Next, we would go through the string, and for each character, we would get the corresponding bits for that character. Then join all the bits together, and then convert to chunks of 8 to get the bytes. If the last byte is not 8 bits long, add zeros at the end till it is 8 bits long. Lastly, convert each byte to its decimal representation.
[Reverse challenge is here.](https://codegolf.stackexchange.com/questions/247669/i-want-8-bits-for-every-character)
## Test cases
```
String: "the fox", encoder characters: " abcdefghijklmnopqrstuvwxyz" => [170, 76, 19, 195, 32]
String: "971428563", encoder characters: "123456789" => [151, 20, 40, 86, 48]
String: "the quick brown fox jumps over the lazy dog", encoder characters: " abcdefghijklmnopqrstuvwxyz" => [170, 76, 25, 89, 68, 96, 71, 56, 97, 225, 60, 50, 21, 217, 209, 160, 97, 115, 76, 53, 73, 130, 209, 111, 65, 44, 16]
String: "abc", encoder characters: "abc" => [108]
String: "aaaaaaaa", encoder characters: "a" => [255]
String: "aaaabbbb", encoder characters: "ab" => [85, 170]
```
## Rules
* Inputs can be a string, list, or even list of character codes. It doesn't matter, I/O is very flexible for this challenge.
* Input will always be valid, e.g. the string will never include characters not in the encoder characters, etc.
* Encoder characters will always contain \*\*less than 256 characters.
* Neither input will ever be empty.
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest answer in bytes for each language wins.
* [Standard I/O rules apply](https://codegolf.meta.stackexchange.com/questions/2419/default-for-code-golf-program-function-or-snippet/2422#2422).
* [Default loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
## Reference implementation in JavaScript
```
function encode(str, encoderChars) {
  const maxBitCount = Math.ceil(Math.log2(encoderChars.length + 1));
  const charToBit = Object.fromEntries(encoderChars.map((c, i) => [c, (i + 1).toString(2).padStart(maxBitCount, "0")]));
  const bits = [...str].map((c) => charToBit[c]).join("");
  const bytes = bits.match(/.{1,8}/g) || [];
  return bytes.map((x) => parseInt(x.padEnd(8, '0'), 2));
}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=pVTNbtNAEBYSp1x4hZEVkbVwndiJE6dRQKLqASToodyMpTjOJnHq7Kbrdes0zRFOvAEg9QAvxbFPwqydH0dtDwhLK6935vvmm_HOfP_F-Ije3f1O5fjI_fPs6zhloYw4A8pCtJBECmOzFyfTQCQ6rCoAIWeJhHmQvY3kCU-ZhD58COTUDGkUk3wX84lNykgzpmwip_AKLF3v7UhCtH3iyIMUZ8MZDaU5Fnx-yqSIaHLIMA8WhIQGRDr0X4OHOxLlfKbk5-jPJsTWzUUwOpeBkKSkzwCtoel-OfAwkgnG9EzTxCz9DXnOvNPkhb5uznjEiKaVoUtJFVZRIE6GU1I3V5bhrusTHW5vwfOVs6AyFazwLuiznH6BudB3TJJMaT1lI-IaUGvUdANspXC9-R3PXxThEJ5LHVSKJI9Bk1MKY55pu5-Taw5CSUWCZgiG4YiOJ9NodhHPGV9cikSmV9fZ8kbLS2d1GgZ02gZYXbUcA5q2v6fvdqyW7Trt5lMBLLvZctodt7uhcyzUjpQtXC7Stlz_UOxlGoUXMBT8minhMEvniwT4FRIrcxzcLGHEJ_-fkI25uJhUG2vaxe8OKnPw3e2gTRnb6OjgspVkS502VA3UsfKxLKcgcpr4xmU1G1sfCyFtNLdaClDKENU9pVyZCoWNck2CzfMkrADZjvMvoGGBclUNEDgwETknupks4kiS2mdW0_OLmCm3bHusPmp6yeJlXsPfWvFqPgY6xmPPQq84CinBwhxhW6OuqyAmGRqw2XqVSn51zTEXpwE2CfG8BwPFN4CncpFKP--N_XQReYu9Pz_7qPoTCxCNl6Q8llTnHjAdjBUuBM6ShwxFMIzV76sQewTjynuLewOaBljSurb3oHM-iw597n98yd3uf37bOfKYqulHBtVVjliDVl2hgLUGL9W2rBnP-tUVhl5j7tXV41rXA70HlbXeK-bCdlz_BQ)
  
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 19 [bytes](https://chat.stackexchange.com/rooms/240/the-nineteenth-byte)
```
¹żb:∩L∆ZĿf8ẇR8∆ZRvB
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLCucW8YjriiKlM4oiGWsS/ZjjhuodSOOKIhlpSdkIiLCIiLCJcIiBhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5elwiXG5cInRoZSBmb3hcIiJd)
```
        Ŀ           # Replace each element of <the key> in <the input>
                    # with the corresponding value of...
¹ż                  # 1...len(key)
  b                 # Convert each to binary
      ∆Z            # left-pad each with zeroes to length
   :∩L              # The maximum length - the length when transposed
         f8ẇ        # Reshape into chunks of 8
            R   R   # With each reversed...
             8∆Z    # Left-pad to length 8 (so right-pad)
                 vB # Convert each back from binary
```
[Answer]
# JavaScript (ES7), ~~124 122~~ 117 bytes
Expects `(encoder_characters)(string)`, where `string` is passed as an array of characters.
```
(b,o=k=[],i=32)=>g=a=>(i+=~Math.log2(b.length),k|=b.indexOf(a.shift())+1<<i)?g(a,i<25&&(o.push(k>>>24),k<<=8,i+=8)):o
```
[Try it online!](https://tio.run/##rZDJasMwFEX3/QrhRSIRVYnlSSmW@wWlHxC8kGfZjuV4yFBKf92VF1k06SKUPnjwQOjewynFUfRxJ9vhuVFJOmV8ghFWvOK7EEtuUcSDnAseQLniX29iKEitcgojUqdNPhQIV588IrJJ0vN7BgXpC5kNEKGV6fsSveZQYOlTZ7GAirRjX8AqCAJq63@@zxnWqQyhFzXFqulVnc7pMIMGEFGcpFleyLKq941qD10/jMfT@fJhILgjhBhDkYJMnY0QkVLJBi4xWCIEHpn1GuxMb4OB52Jgbud1MLBo@HSDYVLLdlyPba@lW8@0KXNc6y@1v2I4JgZUo9h6mcax2R3FgzIOo4wrEHXq1MxiQDnu2x6oY9qB@bkWHxeQqPyW/IcMqj0wLcRlGOhcjWNrO@4dkia6Vs/nP8i4E7Nh4fQN "JavaScript (Node.js) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jellylanguage), 21 bytes
```
JBUz0ZU⁸,yFŻ8¡s8z0ZḊḄ
```
[Try It Online!](https://jht.hyper-neutrino.xyz/tio#WyIiLCJKQlV6MFpV4oG4LHlGxbs4wqFzOHowWuG4iuG4hCIsIiIsIiIsWyIgYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoiLCJ0aGUgZm94Il1d)
```
JBUz0ZU⁸,yFŻ8¡s8z0ZḊḄ  Main Link
J                      Generate [1, 2, ...] as long as the encoding list
 B                     Convert to binary
  Uz0ZU                Reverse, transpose padding with 0, transpose, reverse (*)
       ⁸,              Pair the encoding list with the padded binary list
         y             Use this to translate the message into binary strings
          F            Flatten
           Ż8¡         Prepend 0 8 times (in case the list is shorter than 8)
              s8       Slice into blocks of 8
                z0Z    Transpose padding with 0, transpose (**)
                   Ḋ   Remove the block of 8 zeroes at the start
                    Ḅ  Convert from binary into numbers
(*)  this is a common pattern used to left-pad everything to the same length
(**) this is a common pattern used to right-pad everything to the same length -
     because we prepended a block of 8 zeroes, there is at least one sublist
     of length 8, so everything will be padded to length 8
```
[Answer]
# [Python](https://www.python.org), 115 bytes
```
lambda s,e,o=0:[*sum(-~e.find(x)<<8*(L:=(l:=len(f"{len(e):b}"))*len(s)+7>>3)-(o:=o+l)for x in s).to_bytes(L,'big')]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=pVFLbtswEEW3OcVAm5AObYj6S4h8guy6dIVCH8pmQouKKDl2ivYi3QQo2nt03RO0p-nQjoMsmlUXgwHnPb55j_z6oz-MG909fWvzD9-nsZ0nv40qt1VTgmGC6dzNVjMzbcn8i1i0smvInl5fJzNyk-VEZbkSHWmdT7YJmlWfHUpn9mDoVbxc-nROdJbrK0VbPcAeZAeGLkb9sTqMwpAbdlnJ9SUtnlf_sixlWbpHEZcuBlE2SnbIpdkFAEimIQe1ML2SI3EgX4JDjwCOV_vzPHPoihfwslSeAebQwtJnJ6Ft2ROxKxUjdkCPSv0gu5FI1uKM5rkGPZAVZjSZm2BAq2ms5hEv2L8gXVB6ivT0593P9yMqrjNwxo1AS3uHgehq3YgB6k05lPUoBoMwlFXdiHa9kbd3atvp_n4w47R72B8ej0FXPHYZxBEDntoKGfhecfEin8Y88JIw8t9awD0_CKM4SZ_lQs7AQ8kAK0HZIHmlZs3eT7K-g2rQD501DrfTtjegdyhsYVU-HqDR6_8P5GGWBENFCYMUzzE6C7GnMWIWjJAYYnnWMrdT176BHVsO5-FJKPSxY3HfPXM4XokQDgJ74VVCdPeWcwudHLr4Jqev_As)
## Old [Python](https://www.python.org), 122 bytes
```
def f(s,e,o=0):
 for x in s:o<<=(l:=len(f"{len(e):b}"));o-=~e.find(x)
 l*=len(s);return[*(o<<-l%8).to_bytes(l+7>>3,'big')]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=pZFNbtswEIXRrU8xEFCEdGlD1L-VyJfo0hAK2aJsJrSoULRju2gv0o037T267gna03RoxW0WzaqLAaF5Mx_fo7586452o9vz-evONpPs56kWDTSkZ4Lpwqf5CBpt4ACyhT7Xd3cFUXmhREsa76M7BM2XnzxKb_Wk-CymjWxrcqAjUOPLVE9vjbA70y7GBLcn6m1Gp1Z_WB6t6Il6l87nIbtZyvUNLZ8d_HAXKneh7pDg06kRVa1kiwvOD4BkGgpQ075T0hIPijl49CJge3G49nOPLnj517-8CsyjpRsfD6Bt1RGxrxQjrkEvpM7I1hLJGuzRotCgDVlg5D73M8zrmL1jXvSS_UvSJaVDpPOvN9_fWySuc_DsRqClg8dAtCtdCwOrTWWqlRWmRxmq5Qp_wXoj7x_UttXdo-ntbv90OJ4uQRc89RmkCQM-cxUzCINy9Ac_S3kUZHESvnYBD8IoTtJs9oyLOYMAkRFWhtgoe0FzZh93cvUAS6OfWmcc7nfbrge9R7CTVXU6Qq3X_x8owCwZhkoyBjP8TtFZjOcsRc2JCQ7GWIGzzF3Xd2_g2m6G83gAxSGeWDz0rzMcVxKUo8gtvEiI7l5z7qTBoY9vMvzK3w)
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), 90 bytes
```
L$`(.?)(?=.*(¶.*)(?!$)\1)
$.2*
+`(_+)\1
$1 
 _
_
P^`.+
.+$
7* 
¶
L`.{8}
_
 _
+`_ 
 __
%`_
```
[Try it online!](https://tio.run/##FYpBDoIwFAX37xQ1qUlpk5/UjW4MF2DhAYwtahFQQbEqarwWB@Bite5mMtM5XzW5DiHjVlCaiHRJUowDyYgTnqx1Ak4zCWWFUVHBNQMzMFhtLCmQ4phLhnEAMkufxTem2JU1/89gak0IvnSsaHuwfLvbu@JQVvXxdG7ay7W7@fvj2b/ePw "Retina – Try It Online") Explanation:
```
L$`(.?)(?=.*(¶.*)(?!$)\1)
$.2*
```
Get the 1-indexed indices of all of the input string's characters in the encoder characters, plus the number of encoder characters.
```
+`(_+)\1
$1 
 _
_
```
Convert to binary using the custom base conversion characters  `_`.
```
P^`.+
```
Left-pad all of the binary strings to the width of the binary number of encoder characters.
```
.+$
7* 
```
Replace the binary number of encoder characters with seven spaces.
```
¶
```
Join the binary strings together.
```
L`.{8}
```
Split into strings of length 8, ignoring any trailing spaces.
```
_
 _
+`_ 
 __
%`_
```
Convert from the custom binary to decimal.
[Answer]
# Ruby, ~~128~~ ~~120~~ 118 bytes
First attempt, probably some ways to make it more optimal
```
->s,x{s.map{(x.index(_1)+1).to_s(2).rjust Math.log2(x.size+1).ceil,?0}.join.scan(/.{1,8}/).map{_1.ljust(8,?0).to_i 2}}
```
s should be the character codes of the input string and x should be the character codes of the encoder. Returns an array of character codes.
[Attempt this online!](https://ato.pxeger.com/run?1=jVFNbtNAFFbFqjnFw5WoDe7U4_9UShELYMWGbVNFjj1OJnVs1zNOnaQ-RxfdVAhOwinoaXgvSQUbJEZ6M-Pv5_2MH7837XT99C0f_Wh1fhb_Wp1dKrvbKrZM6q3ZMVlmojMn3HrHLaariTJdizWLVmn4kug5K6qZizIlN4IUqZCF_d7p2aKSJVNpUprnbMvtuD-3diknnBXkNmOU7TJKcPt-X_356NWHr58_MZGk80khSwFZBfd0uR8AKN3YIMoUt66GEbwlgiEqa6bqQmrTGGvDIiWyYpUUJpLWgTs93TVgvrmomoxE3YsIU_5T1AjKlTNT2dARULdamcbJFjP3MB7DyRbtPYwu8YZifC9Vi1T3xh8x_HyAjx2hIrsgQ1cjDTKHvwzwekRjDUSZ7d_i6fmoMvRcQF51xrEByTTNRD6by8VNsSyr-rZRul3ddeuNcXzFI8eGKLSBDykCGzz3emAMI-67cRB6mIC7nh-EUTwkecBtcNHiY8Ro82NUU7HbVqY3MG2qu5IKw6Jd1gqqlWiA6CLZrPGfzP6_IRd7ibGpMLZhiN8RVg7wHEbIERmiMMBwqSVOqEMzEEwazoN9osDDE4N7zouGoyVE2vfJgBNgS9jZbr_iDs2UHBbBCLpBcACnuHZaRGPMgQ1f7x_-Nw)
[Answer]
# [Factor](https://factorcode.org/), 123 bytes
```
[ dup length [1,b] dup last log2 1 + '[ >bin _ 48 pad-head ] map zip substitute concat 8 group [ 8 48 pad-tail bin> ] map ]
```
[Try it online!](https://tio.run/##lZG7bsJAEEV7f8WVFSlFEiTzNInkNkqTJkploWhsr@0Fs7vsAzCIb3fWgCBtpprXmRndKSm3UnffXx@f768grak1IGNkbrAmWw80iYoZrJgWrDmnUOr2UlOkDdMwbOOYyNkNrLR0iosKSjNrW6W5sGi4ZZoag7cgOAbw9nBEaGuGUu5DJOfdCEFZXrCyqvly1ayFVBttrNvu9u0hxOnGzWfReBhPpqM7GQ1H48l0Fs//9vXzN47nK2Ra7kS/C0u3VgZy60/vyw0dWhSy@vcNvuvOnINTcAq6FIVTaJiovFZp9JwtLgkyXgRZDRHhCY8pkowL/GAcQ1HxUjMqsPC6Khy4gnGZsdw6y5BLkZNFfJEVqfeujCXewE9JruCiKy8vSPpo0P0C "Factor – Try It Online")
## Explanation
Takes the string as an array of codepoints and the encoder characters as a string.
```
                        ! { 57 55 49 52 50 56 53 54 51 } "123456789"
dup                     ! { 57 55 49 52 50 56 53 54 51 } "123456789" "123456789"
length                  ! { 57 55 49 52 50 56 53 54 51 } "123456789" 9
[1,b]                   ! { 57 55 49 52 50 56 53 54 51 } "123456789" { 1 2 3 4 5 6 7 8 9 }
dup                     ! { 57 55 49 52 50 56 53 54 51 } "123456789" { 1 2 3 4 5 6 7 8 9 } { 1 2 3 4 5 6 7 8 9 }
last                    ! { 57 55 49 52 50 56 53 54 51 } "123456789" { 1 2 3 4 5 6 7 8 9 } 9
log2                    ! { 57 55 49 52 50 56 53 54 51 } "123456789" { 1 2 3 4 5 6 7 8 9 } 3
1 +                     ! { 57 55 49 52 50 56 53 54 51 } "123456789" { 1 2 3 4 5 6 7 8 9 } 4
'[ >bin _ 48 pad-head ] ! { 57 55 49 52 50 56 53 54 51 } "123456789" { 1 2 3 4 5 6 7 8 9 } [ >bin 4 48 pad-head ]
map                     ! { 57 55 49 52 50 56 53 54 51 } "123456789" { "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" "1001" }
zip                     ! { 57 55 49 52 50 56 53 54 51 } { { 49 "0001" } { 50 "0010" } { 51 "0011" } { 52 "0100" } { 53 "0101" } { 54 "0110" } { 55 "0111" } { 56 "1000" } { 57 "1001" }
substitute              ! { "1001" "0111" "0001" "0100" "0010" "1000" "0101" "0110" "0011" }
concat                  ! "100101110001010000101000010101100011"
8 group                 ! { "10010111" "00010100" "00101000" "01010110" "0011" }
[ 8 48 pad-tail bin> ]  ! { "10010111" "00010100" "00101000" "01010110" "0011" } [ 8 48 pad-tail bin> ]
map                     ! { 151 20 40 86 48 }
```
[Answer]
# [Python 3](https://docs.python.org/3/), ~~133~~ ~~132~~ 131 bytes
```
def f(s,e):w=len(f"{len(e):b}");return[int((''.join(f'{e.find(c)+1:0{w}b}'for c in s)+8*'0')[i:i+8],2)for i in range(0,len(s)*w,8)]
```
[Try it online!](https://tio.run/##fVJdb6swDH3nV1i8QNbcifBNr7o/wtBVS0ObtgsswKBF/PZem657HJLl2OfYh8Rurt2x1sH9vpcVVG7LJVsPm4vUbmVP5DDezTb7a2TXG50r3bmu47yeaoUUZ5KvldJ7t2QrsfamYd7NTlUbKEFpaNkqfXE8h@VqrVZpwX1GmCLMbPVBuh4niZa9DDxlxb2Tbfev7UwLG3Dt7iihqkebg50lIvTTKA4ooPxnr8oz7Ew9aOLAqf9oWqi/pAGCL9vbFfb1gejbXWkza2ktdfloDZjECx@O6nS@fOi6@TRt138N4/VGJcIPwihO0swGjH4jw7cAMEuOjSw7uSeBXCQehyTmIDKyiEPgFxxyEQkOPmIhWop4mC7pJ91HZoolccohwzhBeoQ@SxAjMEZihOZTH0FZjxQoTRwhokejKECPJgLvyRFYEiMchlSwyHppwazyKMszPhz@t/Pe@0lYOhyWkwgcZtHIcC34SGO7qcb9mRKHn1fl8Lw@W1uAH7V7rNMSNoYWB/elnTlMcoY/bzCZGaZv8dxsNmMxO@z@Hw "Python 3 – Try It Online")
*Saved a byte thanks to [Steffan](https://codegolf.stackexchange.com/users/92689/steffan)!!!*
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 26 bytes
```
ＩＥ⪪⭆θ◧⍘⊕⌕ηι !Ｌ↨Ｌη²¦⁸⍘◨ι⁸ !
```
[Try it online!](https://tio.run/##TYzBDoIwDIbvPsXkVBK8eDLxpomJCSZEnmBuZZuOAaMi@vJzgAebNG3//@8nNPei4TaEwhtHcOQ9wYW3ULbWEJQUVTXdXcYKLnOsCA68x8WAsxMea3SEEk7GSdAZM2masYStkzhydIr0/AG/XUd5m06ZXew/VsRfjdIEZrFmRKx9CKSRVc24YvwmJFZKm/vD1q5pO9/Tc3iN70/YDPYL "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
    θ                       Input string
   ⭆                        Map over characters and join
         η                  Encoder characters
        ⌕                   Find 0-based index of
          ι                 Current character
       ⊕                    Incremented
      ⍘                     Custom base conversion using
            !               Literal string ` !`
     ◧                      Left-padded to length
                η           Encoder characters
               Ｌ            Length
              ↨             Converted to base
                 ²          Literal integer `2`
             Ｌ              Length
  ⪪                         Split into substrings of length
                   ⁸        Literal integer `8`
 Ｅ                          Map over substrings
                      ι     Current substring
                     ◨      Right-padded to length
                       ⁸    Literal integer `8`
                    ⍘       Custom base conversion using
                         !  Literal string ` !`
Ｉ                           Cast to string
                            Implicitly print
```
[Answer]
# APL+WIN, 45 bytes
Prompts for string followed by encoder characters:
```
2⊥⍉(m,8)⍴(8×m←⌈(⍴n)÷8)↑n←,⍉((⌈2⍟⌈/n)⍴2)⊤n←⎕⍳⎕
```
[Try it online! Thanks to Dyalog Classic](https://tio.run/##jY3NbcJAEIXvW8Xe1pZAkddgTAlIiVLD4n9Y7xobA6YAAghHySHKMZdQAUgpgE6mEWc2aYDL/LzvzRtRyH7YCKmTfiBFVWVBB68fT4@we3MJTpNnnBwC@5e443A8Q3uw8p5vQ3u1/NtnjhROewtXZd9@UN@9K9R6xmch4dB@YXtQ5oLbcPw2GIOhvWDtMJh0MWHLNKKx3jDCqJgGYRQnaTaby1zpYlFWy3q13jRbRtA5HjkD7g89F70OdwdDb@SP/4jJWNRZMKfTUq@VyaOzOi8qqldRSQ2WYtvQUCd3/EHM/usv "APL (Dyalog Classic) – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 17 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
āb¤g°+€¦‡¾8×«8ô¨C
```
Inputs in reversed order, with the encoding string as first input.
[Try it online](https://tio.run/##AUsAtP9vc2FiaWX//8SBYsKkZ8KwK@KCrMKm4oChwr44w5fCqzjDtMKoQ///IGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6CnRoZSBmb3g) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfWVC8f8jjUmHlqQf2qD9qGnNoWWPGhYe2mdxePqh1RaHtxxa4fxf5390tJJCYlJySmpaekZmVnZObl5@QWFRcUlpWXlFZZWSjlJJRqpCWn6FUqyOQrSSoZGxiamZuYUlUMLS3NDEyMLUzBgiRdiUwtLM5GyFpKL88jyQiQpZpbkFxQr5ZalFCiDpnMSqSoWU/HSIcUDTgNpAZGwsAA).
**Explanation:**
```
ā          # Push a list in the range [1, (implicit) first input-length]
 b         # Convert each to a binary string
  ¤g°+€¦   # Right-pad with 0s to make them all the same length:
  ¤        #  Push the last/longest binary-string
   g       #  Pop and push its length
    °      #  Pop and push 10 to the power this length
     +     #  Add it to each binary
      €¦   #  Remove the leading 1 from each
‡          # Transliterate all characters in the first input to these
           # binary-strings in the second (implicit) input
 ¾8×       # Push a string of 8 0s: "00000000"
    «      # Append the strings together
     8ô    # Split it into parts of size 8
       ¨   # Remove the trailing part
        C  # Convert all binary-strings to integers
           # (after which it is output implicitly as result)
```
`¤g°+€¦` could alternatively be `¤gjð0:` for the same byte-count:
```
  ¤g       #  Same as above to get the length
    j      #  Pad each with leading spaces up to this length
     ð0:   #  Replace all spaces with 0s
```
[Answer]
# [Python 3](https://docs.python.org/3/), 116 bytes
```
lambda s,e,j=''.join:map(lambda*a:int(j(a),2),*[iter(j(f"{e.find(c)+1:0{len(bin(len(e)))-2}b}"for c in s)+7*'0')]*8)
```
[Try it online!](https://tio.run/##jZDBkoIwDIbvPkWmFxrsOgIqyIxPsruHAq0UocUCKjo@O9uOL7A5JJN8//zJpJ/H2uhkkaefpeVdUXEYmGDNKQg2jVE673hPPyDkudIjbShHFiMLv9UorGsleYmNVLqiJa6jfPtqhaaF0tRXgYhf8bt4E2kslKA0DLhOw2Ab4G@Y4eLHbqMHdEXJWAuQ5kEYAV6UlZDnWjWXttOmv9phnG73x/wkyJz0mEa7ONsfEieO4mS3P6TZ8YO8y3VS5QUKa@7aO0Izdf0A5iYseNzy5wyVOf9nk@NO5rNrMV@Bi976Z4SSuuMRlz8 "Python 3 – Try It Online")
Using [xnor's `zip/iter` trick](https://codegolf.stackexchange.com/a/184881/87681) for chunkification (but with `map` instead).
[Answer]
# [PARI/GP](https://pari.math.u-bordeaux.fr), 90 bytes
```
f(s,e)=digits(fromdigits([[i|i<-[1..#e],e[i]==c][1]|c<-s],2^l=#binary(#e))<<(-l*#s%8),256)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=jVBLasMwFKTQgwiZglTsgJ3YccDuMbqoUItsS44SfxTZTuKQm3STTemZ2tNEqlO67du8mTfDDLz3T8W0fCvV5fIx9MKLv14E6lyO00KWsu-Q0G19g4TIs0w84s9mDqcuJ5KmaU6JT8954nXUDV6r1Mlkw_SIHI5xkiCvenS6hxi7QRjhqeH77p4pVY2IAe8JKC2b3kBoCQQCPfMcMZOJXTDBgGJsCCGwX3Mg2iN0jZFlecFFuZabbVU3rdrprh_2h-N4gtSY4WrpL4I4jObW7QfzRRgt49Wk2ZzdIPMtyHR7aGwm2Ay16kC75xpYuWKnERRt-a8uo1ufXRO_zc_x75SZmXyQ0tszft9-BQ)
Inputs lists of characters.
[Answer]
# Desmos, ~~304~~ 265 bytes
```
g=[1...Q][c=s[floor(i/q)+1]][1]
Q=c.length+1
q=ceil(log_2Q)
l=[floor(log_2g)...0]
k=\{q=l[1]+1:[],[2...q-l[1]]0\}
d=[join(k,mod(floor(g/2^l),2))[mod(i,q)+1]fori=[0...qs.length-1]]
v=join(d,[1...8]0)[8u-7...8u]
f(s,c)=[total(2^{[7...0]}v)foru=[1...ceil(d.length/8)]]
```
Both inputs are list of codepoints, as is output. This took me a *long* time.
[Try it on Desmos!](https://www.desmos.com/calculator/xrqd2dr5n6)
*-39 bytes thanks to Aiden Chow*
[Answer]
# [MATL](https://github.com/lmendo/MATL), 17 bytes
```
&m2Gt&mBwY)!8e!XB
```
[Try it online!](https://tio.run/##y00syfn/Xy3XyL1ELdepPFJT0SJVMcLp/3/1RChQ51JPTFIHAA "MATL – Try It Online")
Of note, `B` returns a matrix with the binary strings as rows, so they are automatically padded to the same length.
[Answer]
# [Uiua](https://uiua.org), 30 bytes\*
*or **69 bytes** using native UTF-8 encoding*
```
▽±.⍘⋯≡⇌↯⊂¯1 8≡⇌⬚0⊂∶×0⋯⧻∶⋯+1⊗⊙.
```
[Try it!](https://www.uiua.org/pad?src=Q29tcHJlc3Mg4oaQIOKWvcKxLuKNmOKLr-KJoeKHjOKGr-KKgsKvMSA44omh4oeM4qyaMOKKguKItsOXMOKLr-Knu-KItuKLrysx4oqX4oqZLgpDb21wcmVzcyAidGhlIGZveCIgIiBhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5eiI=)
---
\**See answer to the reverse challenge [here](https://codegolf.stackexchange.com/a/265917/95126) and [here](https://www.uiua.org/pad?src=U0JDUyDihpAgK0BcMOKKlwpEZWNvZGUg4oaQIOKHjOKKjy0x4pa9wrEu4o2Y4ouv4oav4oqCwq8x4qe74ouv4qe7LOKHjOKItSjihpjCrzHii68rMjU2KSAtQFwwKzEKJnAg4oqCIjggYml0IGVuY29kaW5nOlx0IiBTQkNTICLih4ziio8tMeKWvcKxLuKNmOKLr-KGr-KKgsKvMeKnu-KLr-Knuyzih4ziiLUo4oaYwq8x4ouvKzI1NikiIOKKgitAXDDih6ExMjkiLiziiLY74oiYwqzCscKv4oy14oia4peL4oyK4oyI4oGFPeKJoDziiaQ-4omlKy3Dl8O34pe_4oG_4oKZ4oan4oal4oig4qe74paz4oeh4oqi4oeM4pmt4ouv4o2J4o2P4o2W4oqa4oqb4oqd4pah4oqU4omF4oqf4oqC4oqP4oqh4oav4oaZ4oaY4oa74per4pa94oyV4oiK4oqXL-KIp1xc4oi14omh4oi64oqe4oqg4o2l4oqV4oqc4oip4oqT4oqD4oqZ4ouF4o2Y4o2c4o2a4qyaJz_ijaPijaQh4o6L4oas4pqCzrfPgM-E4oiefl9bXXt9KCnCr0AkXCLihpB8IyIKJnAg4oqCImRlY29kZWQ6XHQiIERlY29kZSAiwqPCsS0xwrjChy7DjMKlwrPCsMKIMcKfwqXCnyzCo8K_KMK1wogxwqUrMjU2KSIg4oqCK0BcMOKHoTEyOSIuLOKItjviiJjCrMKxwq_ijLXiiJril4vijIrijIjigYU94omgPOKJpD7iiaUrLcOXw7fil7_igb_igpnihqfihqXiiKDip7vilrPih6HiiqLih4zima3ii6_ijYnijY_ijZbiipriipviip3ilqHiipTiiYXiip_iioLiio_iiqHihq_ihpnihpjihrvil6vilr3ijJXiiIriipcv4oinXFziiLXiiaHiiLriip7iiqDijaXiipXiipziiKniipPiioPiipnii4XijZjijZzijZrirJonP-KNo-KNpCHijovihqzimoLOt8-Az4TiiJ5-X1tde30oKcKvQCRcIuKGkHwjIg==) for a single-byte-character-system (SBCS) encoding.*
[Answer]
# [Julia 1.7](https://julialang.org), 125 bytes
```
s\e=parse.(Int,only.(eachmatch(r"(.{8})",join(string.(findlast.(s,e);base=2,pad=floor(Int,log2(2length(e)))))*"0"^7)),base=2)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=jVBLbtswEEWXySmmXCRUwQimrJ9TqLsuuugJbBegJcqmQ5OKSDt2ipwji268yUl6ivYwRYeymwJddYAhwTfz3rzht5f1VitxPL5sfXtT_nhyM1l1oncypp-MZ9boQ0ylqFcb4esV7QmNv5ZPEWFrqwx1vldmGdNWmUYL52PqmIzeL4STVcI60VSttrYfpLRdJjTR0iz9isooxDsyIl-KKGInQnQ28au1PShQBnopGq2MdDS6BAycx0CaGo99BxW4TitPFYPrmb9-bQkF30-TW2maGz4fYCQhjOc_8CAjd0LTz9KLeFidIhqd1HrpsD5FuTiO5zALCkOhw8W9Hn6AAYHZDMjZGYHqQ3gg9a_G22oYdXX1SiTw_Rk-7jtZe9ncDmyceoneTr9w_PnGEr-S0No9uSAgFnUj2-VKre_0xtjuvnd-u3vYHx7JxZQXIwZFzoBPQmYMxsn8kkwKniZllo9RgCfjNMuLchLaM84gQUqKWSItLbE7DLvfqvoOFr19MGEwrLebzoHdyR5CWYvHAzR2-f-GEvRSoqm8ZDDBd4GTM7wnBdZCMcfGDDMJlnhAR2GHAIcezrOTUDbGG5OPR396OFJyLKdpIOAGaAmdDeeUj8JO4hwBRjDJsjO4wBh6ES1RAw3PTx__Gw)
expects `s` as a list of characters and `e` a string. Needs Julia 1.7 for `only(::RegexMatch)`
]