Base64 decode data: Difference between revisions

added RPL
(added RPL)
 
(41 intermediate revisions by 27 users not shown)
Line 6:
 
When working on the VBA implementation I found several 'solutions' on the net, including one from the software maker himself, that showed output with incorrect padding. Obviously with incorrect padding in the output you can not decode correctly to the original file again.
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE FUNC FindIndex(BYTE b)
IF b>='A AND b<='Z THEN
RETURN (b-'A)
ELSEIF b>='a AND b<='z THEN
RETURN (b-'a+26)
ELSEIF b>='0 AND b<='9 THEN
RETURN (b-'0+52)
ELSEIF b='+ THEN
RETURN (62)
ELSEIF b='/ THEN
RETURN (63)
FI
RETURN (-1)
 
PROC PrintChar(CHAR c)
IF c=10 THEN
PutE()
ELSE
Put(c)
FI
RETURN
 
PROC Decode(CHAR ARRAY s)
BYTE i,b1,b2,b3,b4,i1,i2,i3,i4
CHAR c
 
IF s(0) MOD 4#0 THEN
PrintE("Invalid length of string!!!")
Break()
FI
 
i=1
WHILE i<=s(0)
DO
b1=s(i) i==+1
b2=s(i) i==+1
b3=s(i) i==+1
b4=s(i) i==+1
 
i1=FindIndex(b1)
i2=FindIndex(b2)
 
c=i1 LSH 2
c==%i2 RSH 4
PrintChar(c)
 
IF b3#'= THEN
i3=FindIndex(b3)
c=(i2&$0F) LSH 4
c==%i3 RSH 2
PrintChar(c)
 
IF b4#'= THEN
i4=FindIndex(b4)
c=(i3&$03) LSH 6
c==%i4
PrintChar(c)
FI
FI
OD
RETURN
 
PROC Test(CHAR ARRAY s)
PrintE("Encoded:")
PrintE(s)
PutE()
PrintE("Decoded:")
Decode(s)
PutE()
RETURN
 
PROC Main()
Test("VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Base64_decode_data.png Screenshot from Atari 8-bit computer]
<pre>
Encoded:
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo
 
Decoded:
To err is human, but to really foul things up you need a computer.
--Paul R.Ehrlich
</pre>
 
=={{header|Ada}}==
{{libheader|AWS}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
with AWS.Translator;
Line 22 ⟶ 108:
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line (Result);
end Decode_AWS;</langsyntaxhighlight>
 
{{out}}
Line 32 ⟶ 118:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">text: "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="
 
print decode text</langsyntaxhighlight>
 
{{out}}
Line 40 ⟶ 126:
<pre>To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich</pre>
 
=={{header|BaCon}}==
Using the result from the [[Base64 encode data]] task as requested, but the result is abbreviated in the code below.
<lang bacon>data$ = "AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAE.......QAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAE="
ico$ = B64DEC$(data$)
BSAVE ico$ TO "favicon.ico" SIZE LEN(ico$)</lang>
 
=={{header|Bash}}==
{{trans|Bash}}
<langsyntaxhighlight lang="bash">#! /bin/bash
declare -a encodeTable=(
# + , - . / 0 1 2 3 4 5 6 7 8 9 :
Line 103 ⟶ 183:
fi
done
done</langsyntaxhighlight>
 
=={{header|BASIC}}==
==={{header|BaCon}}===
Using the result from the [[Base64 encode data]] task as requested, but the result is abbreviated in the code below.
<syntaxhighlight lang="bacon">data$ = "AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAE.......QAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAE="
ico$ = B64DEC$(data$)
BSAVE ico$ TO "favicon.ico" SIZE LEN(ico$)</syntaxhighlight>
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 185 ⟶ 272:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo
Line 192 ⟶ 279:
--Paul R.Ehrlich</pre>
 
=={{header|C# sharp|C_sharpC#}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="csharp">using System;
using System.Text;
 
Line 208 ⟶ 295:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 217 ⟶ 304:
=={{header|C++}}==
{{Works with|C++14}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <string>
Line 333 ⟶ 420:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo
Line 346 ⟶ 433:
-- Paul R. Ehrlich
</pre>
 
=={{header|Clojure}}==
 
<syntaxhighlight lang="clojure">(defn decode [str]
(String. (.decode (java.util.Base64/getDecoder) str)))
 
(decode "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=")
</syntaxhighlight>
 
{{out}}
 
<pre>"To err is human, but to really foul things up you need a computer.\n -- Paul R. Ehrlich"</pre>
 
=={{header|Common Lisp}}==
Line 351 ⟶ 450:
Similar to the [http://rosettacode.org/wiki/Base64_encode_data#Common_Lisp BASE64 encoding task] I am using the [http://quickdocs.org/cl-base64/ cl-base64] library.
 
<langsyntaxhighlight lang="lisp">(eval-when (:load-toplevel :compile-toplevel :execute)
(ql:quickload "cl-base64"))
 
Line 373 ⟶ 472:
(len (length array)))
(write-sequence array stream)
(format t "Wrote ~D bytes in file ~A~%" len file))))</langsyntaxhighlight>
{{out}}
 
Line 379 ⟶ 478:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">require "base64"
 
encoded_string = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="
 
decoded_string = Base64.decode_string(encoded_string)
puts decoded_string</langsyntaxhighlight>
 
{{out}}
Line 393 ⟶ 492:
=={{header|D}}==
{{trans|Raku}}
<langsyntaxhighlight lang="d">import std.base64;
import std.stdio;
 
Line 403 ⟶ 502:
auto decoded = cast(char[])Base64.decode(data);
writeln(decoded);
}</langsyntaxhighlight>
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 409 ⟶ 508:
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich</pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">import 'dart:convert';
 
void main() {
var encoded = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=";
var decoded = utf8.decode(base64.decode(encoded));
print(decoded);
}</syntaxhighlight>
 
{{out}}
<pre>
To err is human, but to really foul things up you need a computer.
--Paul R.Ehrlich
</pre>
 
=={{header|Delphi}}==
<langsyntaxhighlight lang="delphi">program Base64Decoder;
 
{$APPTYPE CONSOLE}
Line 426 ⟶ 540:
 
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>Source string:
Line 434 ⟶ 548:
-- Paul R. Ehrlich"</pre>
 
=={{header|F#Ecstasy}}==
<syntaxhighlight lang="java">
/**
* This implementation is encapsulated as Base64Format in the web.xtclang.org module,
* but the module is currently in flux, so the implementation is included "inline" here.
*/
module Base64 {
@Inject Console console;
void run() {
String orig = \|VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIH\
|lvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
;
Byte[] bytes = decode(orig);
String text = encode(bytes, pad=True);
assert text == orig;
console.print($"base64={text}, bytes={bytes}");
}
 
static Byte[] read(Iterator<Char> stream) {
Int charLen = 0;
charLen := stream.knownSize();
Byte[] byteBuf = new Byte[](charLen * 6 / 8);
Byte prevBits = 0;
Int prevCount = 0;
while (Char ch := stream.next()) {
if (Byte newBits := isBase64(ch, assertTrash=True)) {
if (prevCount == 0) {
prevBits = newBits;
prevCount = 6;
} else {
byteBuf.add((prevBits << 8-prevCount) | (newBits >> prevCount-2));
prevBits = newBits;
prevCount -= 2;
}
}
}
 
return byteBuf.freeze(True);
}
 
static void write(Byte[] value, Appender<Char> stream, Boolean pad=False, Int? lineLength=Null) {
lineLength ?:= Int.MaxValue;
 
Int lineOffset = 0;
Int totalChars = 0;
Byte prevByte = 0;
Int prevCount = 0; // number of leftover bits from the previous byte
Int byteOffset = 0;
Int byteLength = value.size;
while (True) {
// glue together the next six bits, which will create one character of output
Byte sixBits;
if (byteOffset >= byteLength) {
if (prevCount == 0) {
break;
}
sixBits = prevByte << 6 - prevCount;
prevCount = 0;
} else if (prevCount == 6) {
sixBits = prevByte << 6 - prevCount;
prevCount = 0;
} else {
Byte nextByte = value[byteOffset++];
sixBits = (prevByte << 6 - prevCount) | (nextByte >> 2 + prevCount);
prevByte = nextByte;
prevCount += 2;
}
 
if (lineOffset >= lineLength) {
stream.add('\r').add('\n');
totalChars += lineOffset;
lineOffset = 0;
}
 
stream.add(base64(sixBits & 0b111111));
++lineOffset;
}
 
if (pad) {
totalChars += lineOffset;
for (Int i = 0, Int padCount = 4 - (totalChars & 0b11) & 0b11; i < padCount; ++i) {
if (lineOffset >= lineLength) {
stream.add('\r').add('\n');
lineOffset = 0;
}
 
stream.add('=');
++lineOffset;
}
}
}
 
static String encode(Byte[] value, Boolean pad=False, Int? lineLength=Null) {
// calculate buffer size
Int byteLen = value.size;
Int charLen = (byteLen * 8 + 5) / 6;
if (pad) {
charLen += 4 - (charLen & 0b11) & 0b11;
}
if (lineLength != Null) {
charLen += ((charLen + lineLength - 1) / lineLength - 1).maxOf(0) * 2;
}
 
StringBuffer charBuf = new StringBuffer(charLen);
write(value, charBuf, pad, lineLength);
return charBuf.toString();
}
 
static Byte[] decode(String text) {
Int charLen = text.size;
Byte[] byteBuf = new Byte[](charLen * 6 / 8);
Byte prevBits = 0;
Int prevCount = 0;
for (Int offset = 0; offset < charLen; ++offset) {
if (Byte newBits := isBase64(text[offset], assertTrash=True)) {
if (prevCount == 0) {
prevBits = newBits;
prevCount = 6;
} else {
byteBuf.add((prevBits << 8-prevCount) | (newBits >> prevCount-2));
prevBits = newBits;
prevCount -= 2;
}
}
}
 
return byteBuf.freeze(True);
}
 
/**
* Translate a single Base64 character to the least significant 6 bits of a `Byte` value.
*
* @param ch the Base64 character; no pad or newlines allowed
*
* @return the value in the range `0 ..< 64`
*/
static Byte valOf(Char ch) {
return switch (ch) {
case 'A'..'Z': (ch - 'A').toUInt8();
case 'a'..'z': (ch - 'a').toUInt8() + 26;
case '0'..'9': (ch - '0').toUInt8() + 52;
case '+': 62;
case '/': 63;
 
case '=': assert as $"Unexpected padding character in Base64: {ch.quoted()}";
case '\r', '\n': assert as $"Unexpected newline character in Base64: {ch.quoted()}";
default: assert as $"Invalid Base64 character: {ch.quoted()}";
};
}
 
/**
* Translate a single Base64 character to the least significant 6 bits of a `Byte` value.
*
* @param ch the character to test if it is Base64
* @param assertTrash (optional) pass True to assert on illegal Base64 characters
*
* @return the value in the range `0 ..< 64`
*/
static conditional Byte isBase64(Char ch, Boolean assertTrash=False) {
return switch (ch) {
case 'A'..'Z': (True, (ch - 'A').toUInt8());
case 'a'..'z': (True, (ch - 'a').toUInt8() + 26);
case '0'..'9': (True, (ch - '0').toUInt8() + 52);
case '+': (True, 62);
case '/': (True, 63);
 
case '=': // "pad" sometimes allowed (or required) at end
case '\r', '\n': // newlines sometimes allowed (or required)
False;
 
default: assertTrash ? assert as $"Invalid Base64 character: {ch.quoted()}" : False;
};
}
 
/**
* Convert the passed byte value to a Base64 character.
*
* @param the byte value, which must be in the range `0..63`
*
* @return the Base64 character
*/
static Char base64(Byte byte) {
return switch (byte) {
case 0 ..< 26: 'A'+byte;
case 26 ..< 52: 'a'+(byte-26);
case 52 ..< 62: '0'+(byte-52);
case 62: '+';
case 63: '/';
default: assert:bounds as $"byte={byte}";
};
}
}
</syntaxhighlight>
 
{{out}}
<pre>
base64=VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=, bytes=0x546F206572722069732068756D616E2C2062757420746F207265616C6C7920666F756C207468696E677320757020796F75206E656564206120636F6D70757465722E0A202020202D2D205061756C20522E204568726C696368
</pre>
 
=={{header|F_Sharp|F#}}==
===Standard Library===
<langsyntaxhighlight lang="fsharp">
open System
open System.IO
Line 445 ⟶ 758:
 
File.WriteAllBytes("favicon.ico", decoded)
</syntaxhighlight>
</lang>
{{out}}
[https://rosettacode.org/favicon.ico Rosetta Code Icon]
 
===Manual Implementation===
<langsyntaxhighlight lang="fsharp">
open System
open System.IO
Line 477 ⟶ 790:
 
File.WriteAllBytes("favicon.ico", decoded)
</syntaxhighlight>
</lang>
{{out}}
[https://rosettacode.org/favicon.ico Rosetta Code Icon]
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: base64 io strings ;
 
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo"
base64> >string print</langsyntaxhighlight>
{{out}}
<pre>
Line 500 ⟶ 813:
Inspired from Wikipedia. Use of a buffer.
May be also of interest : github.com/lietho/base64-forth
<langsyntaxhighlight lang="forth">variable bitsbuff
 
: char>6bits ( c -- u )
Line 534 ⟶ 847:
rot - ( addr2 n2 )
;
</syntaxhighlight>
</lang>
 
{{out}}
Line 557 ⟶ 870:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Dim Shared As String B64
B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & _
"abcdefghijklmnopqrstuvwxyz" & _
Line 589 ⟶ 902:
Print msg64
Print: Print(Decode64(msg64))
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 596 ⟶ 909:
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich
</pre>
 
=={{header|Frink}}==
This reproduces the Rosetta Code icon as specified in the [[Base64 encode data]] task.
<syntaxhighlight lang="frink">
s="""iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC/xh
BQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAA
OpgAABdwnLpRPAAAAWJQTFRFAAAA/8IA/8EA/8IA/8IA/8IA/8IA/8IA/8IA/8IA
/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA
/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8UA/8UA/8QA/8IA/8IA/8IA/8IA/8IA
/8IA/8MAdWVhiHJUiHJUc2Rj/8MA/8IA/8IA/8IA/8IA/8IA06QfjXZQjnZQjXVR
3qwX/8IA/8IA/8IA/8IA/8IA/8kAjHVRjnZQjnZQjHVR/8gA/8IA/8IA/8IAjHVR
jHVR/8gA/8IA/8IA1aYejXZQjnZQjXVR3qwX/8IA/8IA/8IA/8MAdGRjh3FVcmNj
/8MA/8IA/8QA/8UA/8UA/8UA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA
/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IAjnZQ////pZF7sgAAAHN0Uk5T
AAAAA5iNAjzp5DUSMTAQYPz6WBEEjPCUG8G7GZrvhkfqRTV2MUvy50Jc9FoZRUQW
X/vzDau2Gab7nRi6qQwlWyZR9fJIKCMnYVBJK624GqX6nhm8C/VcGEEWYFczdXQv
SvGI7O0awBeXLA9f+VY+5jiZkk/hQmMAAAABYktHRHWoapj7AAAACXBIWXMAAA3X
AAAN1wFCKJt4AAAA7UlEQVQY0z1P11ICARDbFTvIKZ4HooiA7USxYMHGqRSxY6HY
AAULxRr+f1zAMU+ZzCabEAnY1A50dDL9oY27uoGeXm4qbLb0WZV+YMA2KIxJHdI0
u2MYcI6Maq4xE7nHAZfH6/NNTE4B0zOkz8q1f24+sLC4BCzbKLgCrK6th0Ibm1vA
9g5x2DB29/br9Ug0phtxJj5QErHDhnB0nFDCTMET4PTsPJm8uLwSyzXpKQlNZ7LZ
m9tG6B15pKTmvn/I5QuPzbfqU7Fkf34R3+tbqSjF2FouV6pSvfZeEcatcR9S9/OL
/+ey+g38tOb/AjOBNqW00PrwAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE1LTA4LTAy
VDIwOjM5OjEwKzAwOjAw98IZEQAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNS0wOC0w
MlQyMDozOToxMCswMDowMIafoa0AAABGdEVYdHNvZnR3YXJlAEltYWdlTWFnaWNr
IDYuNy44LTkgMjAxNC0wNS0xMiBRMTYgaHR0cDovL3d3dy5pbWFnZW1hZ2ljay5v
cmfchu0AAAAAGHRFWHRUaHVtYjo6RG9jdW1lbnQ6OlBhZ2VzADGn/7svAAAAGHRF
WHRUaHVtYjo6SW1hZ2U6OmhlaWdodAAxOTIPAHKFAAAAF3RFWHRUaHVtYjo6SW1h
Z2U6OldpZHRoADE5MtOsIQgAAAAZdEVYdFRodW1iOjpNaW1ldHlwZQBpbWFnZS9w
bmc/slZOAAAAF3RFWHRUaHVtYjo6TVRpbWUAMTQzODU0Nzk1MNul3mEAAAAPdEVY
dFRodW1iOjpTaXplADBCQpSiPuwAAABWdEVYdFRodW1iOjpVUkkAZmlsZTovLy9t
bnRsb2cvZmF2aWNvbnMvMjAxNS0wOC0wMi8xNDBkYmM4M2RmNWY3NmQyNmIzYWNl
M2ZlYTViYzI5ZS5pY28ucG5nBect2gAAAABJRU5ErkJggg=="""
 
f = newJava["java.io.FileOutputStream", ["favicon.ico"]]
f.write[base64DecodeToBytes[s]]
f.close[]</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSlog.incl"
 
local fn EncodeStringAsBase64( stringToEncode as CFStringRef ) as CFStringRef
CFStringRef encodedBase64Str = NULL
CFDataRef dataToEncode = fn StringData( stringToEncode, NSUTF8StringEncoding )
encodedBase64Str = fn DataBase64EncodedString( dataToEncode, 0 )
end fn = encodedBase64Str
 
 
local fn DecodeBase64String( base64String as CFStringRef ) as CFStringRef
CFStringRef decodedString = NULL
CFDataRef encodedData = fn DataWithBase64EncodedString( base64String, NSDataBase64DecodingIgnoreUnknownCharacters )
decodedString = fn StringWithData( encodedData, NSUTF8StringEncoding )
end fn = decodedString
 
 
CFStringRef originalString, encodedBase64String, decodedBase64String
 
originalString = @"This is a test string to be encoded as Base64 and then decoded."
NSLog( @"This is the original string:\n\t%@\n", originalString )
 
encodedBase64String = fn EncodeStringAsBase64( originalString )
NSLog( @"This is the original string encoded as Base84:\n\t%@\n", encodedBase64String )
 
decodedBase64String = fn DecodeBase64String( encodedBase64String )
NSLog( @"This is the Base64 string decoded:\n\t%@", decodedBase64String )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
This is the original string:
This is a test string to be encoded as Base64 and then decoded.
 
This is the original string encoded as Base84:
VGhpcyBpcyBhIHRlc3Qgc3RyaW5nIHRvIGJlIGVuY29kZWQgYXMgQmFzZTY0IGFuZCB0aGVuIGRlY29kZWQu
 
This is the Base64 string decoded:
This is a test string to be encoded as Base64 and then decoded.
</pre>
 
Line 601 ⟶ 994:
=={{header|Go}}==
As images can no longer be uploaded to RC, I've encoded and decoded a string rather than the Rosetta Code icon.
<langsyntaxhighlight lang="go">package main
 
import (
Line 619 ⟶ 1,012:
}
fmt.Println("\nDecoded :", string(decoded))
}</langsyntaxhighlight>
 
{{out}}
Line 632 ⟶ 1,025:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">import java.nio.charset.StandardCharsets
 
class Decode {
Line 642 ⟶ 1,035:
System.out.println(decodedStr)
}
}</langsyntaxhighlight>
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 649 ⟶ 1,042:
=={{header|Haskell}}==
Simple implementation that decodes an ASCII string.
<langsyntaxhighlight lang="haskell">--Decodes Base64 to ASCII
import qualified Data.Map.Strict as Map (Map, lookup, fromList)
import Data.Maybe (fromJust, listToMaybe, mapMaybe)
Line 688 ⟶ 1,081:
putStrLn $
byteToASCII
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCi0tIFBhdWwgUi4gRWhybGljaA=="</langsyntaxhighlight>
{{Out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 697 ⟶ 1,090:
or in terms of Data.ByteString.Base64:
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
 
import qualified Data.ByteString.Base64 as Base64 (decode, encode)
Line 710 ⟶ 1,103:
either print B.putStrLn $
Base64.decode
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCi0tIFBhdWwgUi4gRWhybGljaA=="</langsyntaxhighlight>
{{Out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCi0tIFBhdWwgUi4gRWhybGljaA==
Line 720 ⟶ 1,113:
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">class Main {
static function main() {
var data = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVw" +
Line 728 ⟶ 1,121:
Sys.println(decoded);
}
}</langsyntaxhighlight>
 
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich</pre>
 
=={{header|J}}==
using J's [https://github.com/jsoftware/convert_misc/blob/master/base64.ijs convert/misc/base64] script:
 
<syntaxhighlight lang="j"> require'convert/misc/base64'
frombase64 'VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g='
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich</syntaxhighlight>
 
Alternative implementation based on that script:
 
<syntaxhighlight lang="j">
BASE64=: (a.{~ ,(a.i.'Aa') +/i.26),'0123456789+/'
 
frombase64=: {{
pad=. _2 >. (y i. '=') - #y
pad }. a. {~ #. _8 [\ , (6#2) #: BASE64 i. y
}}
</syntaxhighlight>
 
=={{header|Java}}==
<p>
Java offers the <code>Base64</code> class, which includes both the <code>Encoder</code> and <code>Decoder</code> classes.<br />
The implementation supports RFC 4648 and RFC 2045.
</p>
<p>
Similar to the encoding process, the usage is very simple, supply a <code>byte</code> array, and it will return an encoded <code>byte</code> array.
</p>
<p>
The class uses a static construct, so instead of instantiation via a constructor, you use one of the <kbd>static</kbd> methods.<br />
In this case we'll use the <code>Base64.getDecoder</code> method to acquire our instance.
</p>
<syntaxhighlight lang="java">
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
</syntaxhighlight>
<syntaxhighlight lang="java">
void decodeToFile(String path, byte[] bytes) throws IOException {
try (FileOutputStream stream = new FileOutputStream(path)) {
byte[] decoded = Base64.getDecoder().decode(bytes);
stream.write(decoded, 0, decoded.length);
}
}
</syntaxhighlight>
The operation is successful, creating a 48 by 48-pixel, 32-bit color graphic, at 15,086 bytes.
<br /><br />
An alternate demonstration
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.nio.charset.StandardCharsets;
import java.util.Base64;
 
Line 747 ⟶ 1,186:
System.out.println(decodedStr);
}
}</langsyntaxhighlight>
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich</pre>
 
=={{header|JavaScript}}==
=== Browser ===
<syntaxhighlight lang="javascript">// define base64 data; in this case the data is the string: "Hello, world!"
const base64 = 'SGVsbG8sIHdvcmxkIQ==';
// atob is a built-in function.
console.log(atob(base64));</syntaxhighlight>
=== Node.js ===
<syntaxhighlight lang="javascript">// define base64 data; in this case the data is the string: "Hello, world!"
const base64 = Buffer.from('SGVsbG8sIHdvcmxkIQ==', 'base64');
// <Buffer>.toString() is a built-in method.
console.log(base64.toString());</syntaxhighlight>
{{out}}
<pre>Hello, world!</pre>
 
=={{header|jq}}==
 
Simply pipe the base64 string into:
 
jq -rR base64d
 
=={{header|Jsish}}==
See [[Base64_encode_data#Jsish]] for ''base64.jsi''.
 
<langsyntaxhighlight lang="javascript">/* Base64 decode, in Jsish */
var data = exec('jsish base64.jsi', {retAll:true}).data; // or use File.read('stdin');
var icon = Util.base64(data, true);
File.write('rosetta-favicon.ico', icon);</langsyntaxhighlight>
 
=={{header|Julia}}==
Using an IOBuffer here, though not really needed to decode a string, shows how we could pipe a network stream or file though Julia's builtin Base64 decoder.
<langsyntaxhighlight lang="julia">using Base64
 
io = IOBuffer()
Line 773 ⟶ 1,232:
 
println(String(read(iob64_decode)))
</langsyntaxhighlight>{{out}}
<pre>
To err is human, but to really foul things up you need a computer.
Line 781 ⟶ 1,240:
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">import java.util.Base64
 
fun main() {
Line 790 ⟶ 1,249:
val decodedStr = String(decoded, Charsets.UTF_8)
println(decodedStr)
}</langsyntaxhighlight>
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 796 ⟶ 1,255:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Start taken from https://stackoverflow.com/a/35303321
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -- You will need this for encoding/decoding
 
Line 821 ⟶ 1,280:
 
local decoded = dec(data)
print(decoded)</langsyntaxhighlight>
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo
Line 828 ⟶ 1,287:
--Paul R.Ehrlich</pre>
 
=={{header|MathematicaM2000 Interpreter}}==
 
<lang Mathematica>ImportString[
=====Using a file and a Document object=====
Load.doc handle automatic UTF16LE UTF16BE UTF8 and ANSI. Also automatic find the type of new line (here is LF)
 
Tempname$ retuen a unique path+filename at temporary folder. The temporary file automatic deleted when the M2000 environment closed.
 
<syntaxhighlight lang="m2000 interpreter">
inp$={VG8gZXJyIGlzIGh1bWFuLCBidXQ
gdG8gcmVhbGx5IGZvdWwgdGhpbmdzI
HVwIHlvdSBuZWVkIGEgY29tcHV0ZXI
uCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
}
tmp$=tempname$
open tmp$ for wide output as #f
print #f, string$(inp$ as decode64);
close #f
document a$
load.doc a$, tmp$
report a$
clipboard a$
</syntaxhighlight>
 
=====Without File=====
If we know the encoding and type of line feed we can do this:
 
<syntaxhighlight lang="m2000 interpreter">
inp$={VG8gZXJyIGlzIGh1bWFuLCBidXQ
gdG8gcmVhbGx5IGZvdWwgdGhpbmdzI
HVwIHlvdSBuZWVkIGEgY29tcHV0ZXI
uCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
}
// decode inp$ (is 8-bit ANSI coded using 0xA for paragraph end/new line)
a$=string$(inp$ as Decode64)
locale 1033
// convert ansi to utf16le using Locale 1033
a$=chr$(a$) // convert to utf16LE
// expand LF to CRLF for M2000 console
a$=replace$(chr$(0xA), chr$(0XD)+chr$(0XA), a$)
// report handle cr and lf using proportional spacing for text and auto word wrapping
report a$
// also Print channel #-2 handle cr and lf (using non proportional spacing)
print #-2, a$
</syntaxhighlight>
 
{{out}}
<pre>
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ImportString[
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo",
"Base64"
]</langsyntaxhighlight>
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 838 ⟶ 1,348:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import base64
 
const Source = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="
 
echo Source.decode()</langsyntaxhighlight>
 
{{out}}
Line 857 ⟶ 1,367:
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(define base64-codes "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
(define kernel (alist->ff (map cons (string->bytes base64-codes) (iota (string-length base64-codes)))))
Line 894 ⟶ 1,404:
 
(decode "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=")
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 907 ⟶ 1,417:
=={{header|Perl}}==
The MIME::Base64 module is to be preferred, but this works too.
<langsyntaxhighlight lang="perl">sub decode_base64 {
my($d) = @_;
$d =~ tr!A-Za-z0-9+/!!cd;
Line 926 ⟶ 1,436:
EOD
 
print decode_base64($data) . "\n";</langsyntaxhighlight>
{{out}}
<pre>'Twas brillig, and the slithy toves
Line 934 ⟶ 1,444:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include builtins\base64.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
string s = "Rosetta Code Base64 decode data task"
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">base64</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
string e = encode_base64(s)
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Rosetta Code Base64 decode data task"</span>
?e
<span style="color: #004080;">string</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">encode_base64</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
?decode_base64(e)</lang>
<span style="color: #0000FF;">?</span><span style="color: #000000;">e</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">decode_base64</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 946 ⟶ 1,459:
 
=={{header|PHP}}==
<langsyntaxhighlight PHPlang="php">$encoded = 'VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVw' .
'IHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=';
echo
$encoded, PHP_EOL,
base64_decode($encoded), PHP_EOL;</langsyntaxhighlight>
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 957 ⟶ 1,470:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(setq *Char64
`'(chop
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ) )
Line 980 ⟶ 1,493:
(link
(char (| (>> -6 (& C 3)) D)) ) ) ) ) ) ) ) )
(prinl (decode64 "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo"))</langsyntaxhighlight>
{{out}}
<pre>
Line 990 ⟶ 1,503:
By necessity this also implements all of the Base64 encode task to
avoid a humongous amount of icon data hardcoded in the program.
<syntaxhighlight lang="pike">
<lang Pike>
string icon = Protocols.HTTP.get_url_data("http://rosettacode.org/favicon.ico");
string encoded = MIME.encode_base64(icon);
Stdio.write_file("favicon.ico", MIME.decode_base64(encoded));
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Line 1,009 ⟶ 1,522:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">b64cd$ = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVw" +
"IHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="
 
Line 1,015 ⟶ 1,528:
Base64Decoder(b64cd$, *p_buf, 1024)
OpenConsole("") : PrintN(PeekS(*p_buf, -1, #PB_UTF8))
Input()</langsyntaxhighlight>
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 1,021 ⟶ 1,534:
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
import base64
data = 'VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g='
print(base64.b64decode(data).decode('utf-8'))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,032 ⟶ 1,545:
</pre>
=={{header|QB64}}==
<syntaxhighlight lang="vb">Option _Explicit
<lang vb>
Option _Explicit
 
Dim Shared As String Base64(0 To 63)
Dim As _Unsigned Integer x
Restore BASE64
For x = 0 To 63
Read Base64(x)
Next
 
Dim As String udata, decoded
Line 1,049 ⟶ 1,554:
Print udata
Print decoded
 
BASE64:
Data "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
Data "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"
Data "0","1","2","3","4","5","6","7","8","9","+","/"
 
Function findIndex& (value As _Unsigned _Byte)
Line 1,080 ⟶ 1,580:
 
Function encode$ (source As String)
Dim As String Base64: Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim As _Unsigned _Integer64 length: length = Len(source)
Dim As _Unsigned _Integer64 it, strend
Line 1,089 ⟶ 1,590:
it = it + 1
b1 = Asc(Mid$(source, it, 1))
sink = sink + Base64Mid$(Base64, _SHR(b1, 2), 1)
acc = _SHL(b1 And &H3, 4)
If it <> strend Then
Line 1,095 ⟶ 1,596:
b2 = Asc(Mid$(source, it, 1))
acc = acc Or _SHR(b2, 4)
sink = sink + Base64Mid$(Base64, acc, 1)
acc = _SHL(b2 And &HF, 2)
If it <> strend Then
Line 1,101 ⟶ 1,602:
b3 = Asc(Mid$(source, it, 1))
acc = acc Or _SHR(b3, 6)
sink = sink + Base64Mid$(Base64, acc, 1)
sink = sink + Base64Mid$(Base64, b3 And &H3F, 1)
Else
sink = sink + Base64Mid$(Base64, acc, 1)
sink = sink + "="
End If
Else
sink = sink + Base64Mid$(Base64, acc, 1)
sink = sink + "="
sink = sink + "="
Line 1,154 ⟶ 1,655:
Wend
decode = sink
End Function</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,161 ⟶ 1,661:
To err is human, but to really foul things up you need a computer.
--Paul R.Ehrlich
</pre>
 
=={{header|R}}==
===Using base64enc===
{{works with|R|4.1.0}}
{{libheader|base64enc}}
<syntaxhighlight lang="rsplus">
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo" |>
base64enc::base64decode() |>
rawToChar() |>
cat()
</syntaxhighlight>{{out}}
<pre>
To err is human, but to really foul things up you need a computer.
--Paul R.Ehrlich
</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
(require net/url net/base64)
 
;; decode a string
(displayln (base64-decode #"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="))
 
;; (encode and) decode the favicon
(displayln (base64-decode (base64-encode (call/input-url (string->url "https://rosettacode.org/favicon.ico")
get-pure-port port->bytes))))
</syntaxhighlight>
{{out}}
(string output only)
<pre>
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich
</pre>
 
Line 1,167 ⟶ 1,701:
{{works with|Rakudo|2018.11}}
 
<syntaxhighlight lang="raku" perl6line>my $e64 = '
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY2
9tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 1,192 ⟶ 1,726:
say "\nFast:";
use Base64::Native;
say base64-decode($e64).decode('utf8');</langsyntaxhighlight>
{{out}}
<pre>Slow:
Line 1,203 ⟶ 1,737:
 
=={{header|Red}}==
<langsyntaxhighlight lang="red">Red [Source: https://github.com/vazub/rosetta-red]
 
print to-string debase "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo"
</syntaxhighlight>
</lang>
{{out}}
<pre>
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
#======================================#
# Sample: Base64 decode data
# Author: Gal Zsolt, Mansour Ayouni
#======================================#
 
load "guilib.ring"
 
oQByteArray = new QByteArray()
oQByteArray.append("Rosetta Code Base64 decode data task")
oba = oQByteArray.toBase64().data()
see oba + nl
 
oQByteArray = new QByteArray()
oQByteArray.append(oba)
see oQByteArray.fromBase64(oQByteArray).data()
</syntaxhighlight>
{{out}}
<pre>
Um9zZXR0YSBDb2RlIEJhc2U2NCBkZWNvZGUgZGF0YSB0YXNr
Rosetta Code Base64 decode data task
</pre>
 
=={{header|RPL}}==
{{works with|RPL|HP-49C}}
« "" DUP DUP2 → s a b c d
« ""
1 s SIZE '''FOR''' j
0 3 '''FOR''' k
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
s j k + DUP SUB POS
'''NEXT'''
4 →LIST 1 - { a b c d } STO
a 4 * b 16 / IP + CHR +
'''IF''' c -1 ≠ '''THEN''' b 16 MOD 16 * c 4 / IP + CHR + '''END'''
'''IF''' d -1 ≠ '''THEN''' c 4 MOD 64 * d + CHR + '''END'''
4 '''STEP'''
» » '<span style="color:blue">B64→</span>' STO
 
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=" <span style="color:blue">B64→</span>
{{out}}
<pre>
1: "To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich"
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'base64'
 
raku_example ='
Line 1,220 ⟶ 1,801:
9tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
'
puts Base64.decode64 raku_example</langsyntaxhighlight>
{{out}}
<pre>
Line 1,228 ⟶ 1,809:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::str;
 
const INPUT: &str = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo";
Line 1,265 ⟶ 1,846:
 
println!("Output: {}", result);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,275 ⟶ 1,856:
=={{header|Scala}}==
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/mjgxJDp/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/P4RfGhRQSkaKWEmdBi1gaw Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">import java.util.Base64
 
object Base64Decode extends App {
Line 1,288 ⟶ 1,869:
 
println(text2BinaryDecoding(data))
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 1,295 ⟶ 1,876:
[http://seed7.sourceforge.net/libraries/encoding.htm#fromBase64(in_string) fromBase64].
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "gethttp.s7i";
include "encoding.s7i";
Line 1,308 ⟶ 1,889:
writeln("Is the Rosetta Code icon the same (byte for byte) encoded then decoded: " <&
fromBase64(encoded) = original);
end func;</langsyntaxhighlight>
 
{{out}}
<pre>Is the Rosetta Code icon the same (byte for byte) encoded then decoded: TRUE</pre>
 
=={{header|SenseTalk}}==
<syntaxhighlight lang="sensetalk">
 
put base64Decode ("VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuIC0tUGF1bCBSLkVocmxpY2g=")
 
</syntaxhighlight>
 
Output:
<syntaxhighlight lang="sensetalk">To err is human, but to really foul things up you need a computer. --Paul R.Ehrlich</syntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var data = <<'EOT'
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY2
9tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
EOT
 
say data.decode_base64</langsyntaxhighlight>
{{out}}
<pre>
Line 1,328 ⟶ 1,919:
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">data := '
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY2
9tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
'.
 
data base64Decoded asString printCR.</langsyntaxhighlight>
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 1,339 ⟶ 1,930:
 
=={{header|Standard ML}}==
<syntaxhighlight lang Standard ML="sml">val debase64 = fn input =>
let
 
Line 1,369 ⟶ 1,960:
String.implode ( List.map (chr o Word.toInt) ( decodeLst (String.explode input )) )
 
end handle Subscript => "Invalid code\n" ;</langsyntaxhighlight>
call
<pre>
Line 1,377 ⟶ 1,968:
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich </pre>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">
import Foundation
 
let input = """
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
"""
 
if let decoded = Data(base64Encoded: input),
let str = String(data: decoded, encoding: .utf8) {
print( str )
}
</syntaxhighlight>
<pre>
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich
</pre>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require tcl 8.6
set data VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
 
puts [binary decode base64 $data]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,390 ⟶ 2,000:
=={{header|Visual Basic .NET}}==
{{trans|D}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Sub Main()
Line 1,401 ⟶ 2,011:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 1,407 ⟶ 2,017:
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import encoding.base64
 
fn main() {
msg := "Rosetta Code Base64 decode data task"
println("Original : $msg")
encoded := base64.encode_str(msg)
println("\nEncoded : $encoded")
decoded := base64.decode_str(encoded)
println("\nDecoded : $decoded")
}</syntaxhighlight>
 
{{out}}
<pre>
Original : Rosetta Code Base64 decode data task
 
Encoded : Um9zZXR0YSBDb2RlIEJhc2U2NCBkZWNvZGUgZGF0YSB0YXNr
 
Decoded : Rosetta Code Base64 decode data task
</pre>
 
=={{header|Wren}}==
Line 1,412 ⟶ 2,043:
{{libheader|Wren-str}}
From first principles using string manipulation. Quick enough here.
<langsyntaxhighlight ecmascriptlang="wren">import "io" for Stdout
import "./fmt" for Conv, Fmt
import "./str" for Str
 
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Line 1,441 ⟶ 2,072:
var s = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="
for (chunk in Str.chunks(s, 4)) decode.call(chunk)
System.print()</langsyntaxhighlight>
 
{{out}}
Line 1,448 ⟶ 2,079:
-- Paul R. Ehrlich
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func Decode(Ch);
int Ch;
[if Ch>=^A & Ch<=^Z then return Ch - ^A;
if Ch>=^a & Ch<=^z then return Ch - ^a + 26;
if Ch>=^0 & Ch<=^9 then return Ch - ^0 + 52;
if Ch = ^+ then return 62;
if Ch = ^/ then return 63;
return -1;
];
 
char Str;
int Count, N, Ch, Code, Acc;
[Str:=
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEg
Y29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo ";
loop [Count:= 0;
for N:= 1 to 4 do \read in four 6-bit chars
[repeat Ch:= Str(0); \strip out whitespace
Str:= Str+1;
if Ch = $A0 then quit; \terminating space
until Ch > $20;
Code:= Decode(Ch);
Acc:= Acc<<6;
if Code >= 0 then \don't count possible "="
[Acc:= Acc + Code;
Count:= Count+1;
];
];
ChOut(0, Acc>>16); \write out three 8-bit chars
if Count >= 3 then ChOut(0, Acc>>8);
if Count >= 4 then ChOut(0, Acc);
];
]</syntaxhighlight>
{{out}}
<pre>
To err is human, but to really foul things up you need a computer.
--Paul R.Ehrlich</pre>
 
=={{header|zkl}}==
Using shared libraries for cURL and message hashing:
<langsyntaxhighlight lang="zkl">var [const] MsgHash=Import("zklMsgHash"), Curl=Import("zklCurl");
icon:=Curl().get("http://rosettacode.org/favicon.ico"); //-->(Data(4,331),693,0)
Line 1,459 ⟶ 2,129:
File("rosettaCodeIcon.ico","wb").write(iconDecoded); # eyeball checking says good
println("Is the Rosetta Code icon the same (byte for byte) encoded then decoded: ",
icon==iconDecoded);</langsyntaxhighlight>
{{out}}
<pre>
Line 1,465 ⟶ 2,135:
</pre>
{{out|Text based test}}
<langsyntaxhighlight lang="zkl">msg,b64 := "Rosetta Code Base64 decode data task", MsgHash.base64encode(msg);
println("Original: %s\nEncoded: %s\nBytes: %s\nDecoded: %s"
.fmt(msg, b64.text, b64.bytes().apply("toString",16).concat(","),
MsgHash.base64decode(b64).text));</langsyntaxhighlight>
<pre>
Original: Rosetta Code Base64 decode data task
1,150

edits