I noticed that there is no APL solution in RosettaCode for Base64 encode/decode. I have my own implementation of them that I quickly rolled out, but I'm sure the community has better solutions. Curious if anyone has nice Base64 encode/decode code, and secondly, could someone fluent in J tell me if those J solutions are really complete? https://rosettacode.org/wiki/Base64_decode_data#J https://rosettacode.org/wiki/Base64_encode_data#J
Base64Encode←{
⍝ ⍵ ←→ Text to encode
⍝ ← ←→ Base64 encoded text
b←∊(8⍴2)∘⊤¨⎕UCS ⍵
PadDelta←{⍵-⍨⍺×⌈⍵÷⍺}
Pad←{d x←⍺ ⋄ ⍵,(d PadDelta≢⍵)⍴x}
p←{⍵⊂⍨(≢⍵)⍴6↑1}6 0 Pad b
t←⎕A,⎕C ⎕A,⎕D,'+/'
4 '='Pad t[⎕IO+2⊥¨p]
}
Base64Decode←{
⍝ ⍵ ←→ Base64 encoded text
⍝ ← ←→ Original text
t←⎕A,⎕C ⎕A,⎕D,'+/'
i←(t⍳⍵)-⎕IO
b←∊(6⍴2)∘⊤¨i
p←{⍵⊂⍨(≢⍵)⍴8↑1}b
XPadding←⍵∘{⍵↓⍨-+/∧\⌽'='=⍺}
⎕UCS 2⊥¨XPadding p
}
The J solutions:
BASE64=: (a.{~ ,(a.i.'Aa') +/i.26),'0123456789+/'
frombase64=: {{
pad=. _2 >. (y i. '=') - #y
pad }. a. {~ #. _8 [\ , (6#2) #: BASE64 i. y
}}
base64←{⎕IO ⎕ML←0 1 ⍝ Base64 encoding and decoding as used in MIME.
chars←'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
bits←{,⍉(⍺⍴2)⊤⍵} ⍝ encode each element of ⍵ in ⍺ bits,
⍝ and catenate them all together
part←{((⍴⍵)⍴⍺↑1)⊂⍵} ⍝ partition ⍵ into chunks of length ⍺
0=2|⎕DR ⍵:2∘⊥∘(8∘↑)¨8 part{(-8|⍴⍵)↓⍵}6 bits{(⍵≠64)/⍵}chars⍳⍵
⍝ decode a string into octets
@Adám @Adám basically I'm using Wayland instead of X11, which doesn't work with RIDE (as you may know already: github.com/Dyalog/ride/issues/879). In any case I'm moving to a much more text heavy workflow to minimise distractions b.c. my relationship to time is fragmentary b.c. my employment situation is poor/precarious.