Skip to main content
edited body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
~          # Interpret the input string.
.,~)3%:P   # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
[0]*+      # Append that many zero bytes to the input array.
[4]3*\+    # Prepend 3 bytes to the input array to avoid issues with leading zeros.
256base    # Convert the input array into an integer.
64base     # Convert that integer to base 64.
{          # For each digit:
  '+/'     # Push '+/'.
  'A[a{:0' # Push 'A[a{:0'.
  {        # For each byte in 'A[a{:0':
    ,      # Push the array of all bytes up to that byte.
    ^      # Take the symmetric difference with the array below it.
  }/       # Result: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  =        # Retrieve the character corresponding to the digit.
 }/         #
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]          # Collect all bytes on the stack into an array.
4>         # Remove the first four, which correspond to the 3 prepended bytes.
76/        # Collect all bytes on the stack into an array and split into 76-byte chunks.
"\r\n":n*  # Join the chunks with separator CRLF and save CRLF as the new line terminator.
~          # Interpret the input string.
.,~)3%:P   # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
[0]*+      # Append that many zero bytes to the input array.
[4]3*\+    # Prepend 3 bytes to the input array to avoid issues with leading zeros.
256base    # Convert the input array into an integer.
64base     # Convert that integer to base 64.
{          # For each digit:
  '+/'     # Push '+/'.
  'A[a{:0' # Push 'A[a{:0'.
  {        # For each byte in 'A[a{:0':
    ,      # Push the array of all bytes up to that byte.
    ^      # Take the symmetric difference with the array below it.
  }/       # Result: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  =        # Retrieve the character corresponding to the digit.
 }/        #
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]          # Collect all bytes on the stack into an array.
4>         # Remove the first four, which correspond to the 3 prepended bytes.
76/        # Collect all bytes on the stack into an array and split into 76-byte chunks.
"\r\n":n*  # Join the chunks with separator CRLF and save CRLF as the new line terminator.
~          # Interpret the input string.
.,~)3%:P   # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
[0]*+      # Append that many zero bytes to the input array.
[4]3*\+    # Prepend 3 bytes to the input array to avoid issues with leading zeros.
256base    # Convert the input array into an integer.
64base     # Convert that integer to base 64.
{          # For each digit:
  '+/'     # Push '+/'.
  'A[a{:0' # Push 'A[a{:0'.
  {        # For each byte in 'A[a{:0':
    ,      # Push the array of all bytes up to that byte.
    ^      # Take the symmetric difference with the array below it.
  }/       # Result: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  =        # Retrieve the character corresponding to the digit.
}/         #
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]          # Collect all bytes on the stack into an array.
4>         # Remove the first four, which correspond to the 3 prepended bytes.
76/        # Collect all bytes on the stack into an array and split into 76-byte chunks.
"\r\n":n*  # Join the chunks with separator CRLF and save CRLF as the new line terminator.
Post Undeleted by Dennis
added 598 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

GolfScript, 8480 (8177) bytes

~91,65>123,97>58,48>'+/'+++:B;~.,~)3%:P{0+}*256baseP[0]*+[4]3*\+256base 64base{B='+/''A[a{:0'{,^}/=}/{;}P*'='P*]76P*'='P*]4>76/"\r
":n*
~91,65>123,97>58,48>'+/'+++:B;~.,~)3%:P{0+}*256baseP[0]*+[4]3*\+256base 64base{B='+/''A[a{:0'{,^}/=}/{;}P*'='P*]P*'='P*]4>{13]n+}/
$ echo '[99 97 102 195 169]' | golfscript base64.gs | cat -A
Y2Fmw6k=^M$
$ echo [ {0..142} ] | golfscript base64.gs | cat -A
AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4^M$
OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx^M$
cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY4=^M$
$ echo '[99 97 102 195 169]' | golfscript base64-sneaky.gs | cat -A
Y^M$
2^M$
F^M$
m^M$
w^M$
6^M$
k^M$
=^M$
$
~          # Interpret the input string.
91.,65>  ~)3%:P   # PushCalculate the arraynumber correspondingof bytes missing to theyield stringa 'A...Z'multiple of 3 and save in “P”.
123,97>[0]*+      # PushAppend thethat arraymany correspondingzero bytes to the stringinput 'a...z'array.
58,48> [4]3*\+    # PushPrepend 3 bytes to the arrayinput correspondingarray to theavoid stringissues '0...9'with leading zeros.
'+/'+++:B;256base    # ConcatenateConvert the three previousinput array withinto '+/'an andinteger.
64base save it in “B” # Convert that integer to base 64.
.,~)3%:P{          # CalculateFor theeach numberdigit:
 of bytes'+/' missing to yield a multiple# ofPush 3'+/'.
 and save'A[a{:0' in# “P”Push 'A[a{:0'.
  {0+}*        # AddFor thateach manybyte zeroin bytes'A[a{:0':
 to the input array.
256base,      # ConvertPush the input array intoof anall integerbytes up to that byte.
64base    ^      # ConvertTake thatthe integersymmetric todifference basewith 64the array below it.
{B=  }/       # ForResult: each'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
 digit, retrieve=        # Retrieve the character corresponding characterto fromthe “B”digit.
 }/        #
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]76]          # Collect all bytes on the stack into an array.
4>         # Remove the first four, which correspond to the 3 prepended bytes.
76/        # Collect all bytes on the stack into an array and split into 76-byte chunks.
"\r\n":n*  # Join the chunks with separator CRLF and save CRLF as the new line terminator.

GolfScript, 84 (81) bytes

~91,65>123,97>58,48>'+/'+++:B;.,~)3%:P{0+}*256base 64base{B=}/{;}P*'='P*]76/"\r
":n*
~91,65>123,97>58,48>'+/'+++:B;.,~)3%:P{0+}*256base 64base{B=}/{;}P*'='P*]{13]n+}/
$ echo '[99 97 102 195 169]' | golfscript base64.gs | cat -A
Y2Fmw6k=^M$
$ echo '[99 97 102 195 169]' | golfscript base64-sneaky.gs | cat -A
Y^M$
2^M$
F^M$
m^M$
w^M$
6^M$
k^M$
=^M$
$
~          # Interpret the input string.
91,65>     # Push the array corresponding to the string 'A...Z'.
123,97>    # Push the array corresponding to the string 'a...z'.
58,48>     # Push the array corresponding to the string '0...9'.
'+/'+++:B; # Concatenate the three previous array with '+/' and save it in “B”.
.,~)3%:P   # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
{0+}*      # Add that many zero bytes to the input array.
256base    # Convert the input array into an integer.
64base     # Convert that integer to base 64.
{B=}/      # For each digit, retrieve the corresponding character from “B”.
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]76/       # Collect all bytes on the stack into an array and split into 76-byte chunks.
"\r\n":n*  # Join the chunks with separator CRLF and save CRLF as the new line terminator.

GolfScript, 80 (77) bytes

~.,~)3%:P[0]*+[4]3*\+256base 64base{'+/''A[a{:0'{,^}/=}/{;}P*'='P*]4>76/"\r
":n*
~.,~)3%:P[0]*+[4]3*\+256base 64base{'+/''A[a{:0'{,^}/=}/{;}P*'='P*]4>{13]n+}/
$ echo '[99 97 102 195 169]' | golfscript base64.gs | cat -A
Y2Fmw6k=^M$
$ echo [ {0..142} ] | golfscript base64.gs | cat -A
AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4^M$
OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx^M$
cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY4=^M$
$ echo '[99 97 102 195 169]' | golfscript base64-sneaky.gs | cat -A
Y^M$
2^M$
F^M$
m^M$
w^M$
6^M$
k^M$
=^M$
$
~          # Interpret the input string.
.,~)3%:P   # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
[0]*+      # Append that many zero bytes to the input array.
[4]3*\+    # Prepend 3 bytes to the input array to avoid issues with leading zeros.
256base    # Convert the input array into an integer.
64base     # Convert that integer to base 64.
{          # For each digit:
  '+/'     # Push '+/'.
  'A[a{:0' # Push 'A[a{:0'.
  {        # For each byte in 'A[a{:0':
    ,      # Push the array of all bytes up to that byte.
    ^      # Take the symmetric difference with the array below it.
  }/       # Result: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  =        # Retrieve the character corresponding to the digit.
 }/        #
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]          # Collect all bytes on the stack into an array.
4>         # Remove the first four, which correspond to the 3 prepended bytes.
76/        # Collect all bytes on the stack into an array and split into 76-byte chunks.
"\r\n":n*  # Join the chunks with separator CRLF and save CRLF as the new line terminator.
Post Deleted by Dennis
added non-sneaky version
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

GolfScript, 8584 (81) bytes

~91,65,-.[{32+}/]5865>123,4897>58,-'+48>'+/'+++:B;.,~)3%:P{0+}*256base 64base{B=}/{;}P*'='P*]{13]n+}P*'='P*]76/"\r
":n*

The above will fit exactly 76 characters in a line, except for the last line. All lines are terminated by CRLF.

Note that RFC 2045 specifies a variable, maximumvariable, maximum line length of 76 characters, so at the cost of pretty output, we can save 3 additional bytes.

~91,65>123,97>58,48>'+/'+++:B;.,~)3%:P{0+}*256base 64base{B=}/{;}P*'='P*]{13]n+}/

This codeThe above will print one character per line, except for the last line, which can contain 0, 1 or 2 “=”= chars. GolfScript will also append a final LF, which, according to RFC 2045, must be ignored by decoding software.

Example

$ echo '[99 97 102 195 169]' | golfscript base64.gs | cat -A
Y2Fmw6k=^M$
$ echo '[99 97 102 195 169]' | golfscript base64-sneaky.gs | cat -A
Y^M$
2^M$
F^M$
m^M$
w^M$
6^M$
k^M$
=^M$
$

How it works

~          # Interpret the input string.
91,65,-65>     # Push the array corresponding to the string 'A...Z'.
.[{32+}/]123,97>    # Push the array corresponding to the string 'a...z'.
58,48,-48>     # Push the array corresponding to the string '0...9'.
'+/'+++:B; # Concatenate the three previous array with '+/' and save it in “B”.
.,~)3%:P   # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
{0+}*      # Add that many zero bytes to the input array.
256base    # Convert the input array into an integer.
64base     # Convert that integer to base 64.
{B=}/      # For each digit, retrieve the corresponding character from “B”.
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]   ]76/       # Collect all charactersbytes on the stack into an array.
{13]n+}/ and split #into After76-byte eachchunks.
"\r\n":n* character, form# Join the stringchunks ofwith allseparator charactersCRLF plusand save CRLF as the new line terminator.

GolfScript, 85 bytes

~91,65,-.[{32+}/]58,48,-'+/'+++:B;.,~)3%:P{0+}*256base 64base{B=}/{;}P*'='P*]{13]n+}/

Note that RFC 2045 specifies a variable, maximum line length of 76 characters.

This code will print one character per line, except for the last line, which can contain 0, 1 or 2 “=” chars. GolfScript will also append a final LF, which, according to RFC 2045, must be ignored by decoding software.

Example

$ echo '[99 97 102 195 169]' | golfscript base64.gs | cat -A
Y^M$
2^M$
F^M$
m^M$
w^M$
6^M$
k^M$
=^M$
$

How it works

~          # Interpret the input string.
91,65,-    # Push the array corresponding to the string 'A...Z'.
.[{32+}/]  # Push the array corresponding to the string 'a...z'.
58,48,-    # Push the array corresponding to the string '0...9'.
'+/'+++:B; # Concatenate the three previous array with '+/' and save it in “B”.
.,~)3%:P   # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
{0+}*      # Add that many zero bytes to the input array.
256base    # Convert the input array into an integer.
64base     # Convert that integer to base 64.
{B=}/      # For each digit, retrieve the corresponding character from “B”.
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]          # Collect all characters on the stack into an array.
{13]n+}/   # After each character, form the string of all characters plus CRLF.

GolfScript, 84 (81) bytes

~91,65>123,97>58,48>'+/'+++:B;.,~)3%:P{0+}*256base 64base{B=}/{;}P*'='P*]76/"\r
":n*

The above will fit exactly 76 characters in a line, except for the last line. All lines are terminated by CRLF.

Note that RFC 2045 specifies a variable, maximum line length of 76 characters, so at the cost of pretty output, we can save 3 additional bytes.

~91,65>123,97>58,48>'+/'+++:B;.,~)3%:P{0+}*256base 64base{B=}/{;}P*'='P*]{13]n+}/

The above will print one character per line, except for the last line, which can contain 0, 1 or 2 = chars. GolfScript will also append a final LF, which, according to RFC 2045, must be ignored by decoding software.

Example

$ echo '[99 97 102 195 169]' | golfscript base64.gs | cat -A
Y2Fmw6k=^M$
$ echo '[99 97 102 195 169]' | golfscript base64-sneaky.gs | cat -A
Y^M$
2^M$
F^M$
m^M$
w^M$
6^M$
k^M$
=^M$
$

How it works

~          # Interpret the input string.
91,65>     # Push the array corresponding to the string 'A...Z'.
123,97>    # Push the array corresponding to the string 'a...z'.
58,48>     # Push the array corresponding to the string '0...9'.
'+/'+++:B; # Concatenate the three previous array with '+/' and save it in “B”.
.,~)3%:P   # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
{0+}*      # Add that many zero bytes to the input array.
256base    # Convert the input array into an integer.
64base     # Convert that integer to base 64.
{B=}/      # For each digit, retrieve the corresponding character from “B”.
{;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
]76/       # Collect all bytes on the stack into an array and split into 76-byte chunks.
"\r\n":n*  # Join the chunks with separator CRLF and save CRLF as the new line terminator.
added 102 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading