Base64 decode data: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 8:
=={{header|Action!}}==
<
IF b>='A AND b<='Z THEN
RETURN (b-'A)
Line 81:
PROC Main()
Test("VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo")
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Base64_decode_data.png Screenshot from Atari 8-bit computer]
Line 95:
=={{header|Ada}}==
{{libheader|AWS}}
<
with AWS.Translator;
Line 108:
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line (Result);
end Decode_AWS;</
{{out}}
Line 118:
=={{header|Arturo}}==
<
print decode text</
{{out}}
Line 129:
=={{header|BaCon}}==
Using the result from the [[Base64 encode data]] task as requested, but the result is abbreviated in the code below.
<
ico$ = B64DEC$(data$)
BSAVE ico$ TO "favicon.ico" SIZE LEN(ico$)</
=={{header|Bash}}==
{{trans|Bash}}
<
declare -a encodeTable=(
# + , - . / 0 1 2 3 4 5 6 7 8 9 :
Line 189:
fi
done
done</
=={{header|C}}==
{{trans|C++}}
<
#include <stdlib.h>
Line 271:
return 0;
}</
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo
Line 280:
=={{header|C sharp|C#}}==
{{trans|Visual Basic .NET}}
<
using System.Text;
Line 294:
}
}
}</
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 303:
=={{header|C++}}==
{{Works with|C++14}}
<
#include <iostream>
#include <string>
Line 419:
return 0;
}</
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo
Line 435:
=={{header|Clojure}}==
<
(String. (.decode (java.util.Base64/getDecoder) str)))
(decode "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=")
</syntaxhighlight>
{{out}}
Line 449:
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.
<
(ql:quickload "cl-base64"))
Line 471:
(len (length array)))
(write-sequence array stream)
(format t "Wrote ~D bytes in file ~A~%" len file))))</
{{out}}
Line 477:
=={{header|Crystal}}==
<
encoded_string = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="
decoded_string = Base64.decode_string(encoded_string)
puts decoded_string</
{{out}}
Line 491:
=={{header|D}}==
{{trans|Raku}}
<
import std.stdio;
Line 501:
auto decoded = cast(char[])Base64.decode(data);
writeln(decoded);
}</
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 509:
=={{header|Dart}}==
<
void main() {
Line 515:
var decoded = utf8.decode(base64.decode(encoded));
print(decoded);
}</
{{out}}
Line 524:
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 539:
end.
</syntaxhighlight>
{{out}}
<pre>Source string:
Line 549:
=={{header|F_Sharp|F#}}==
===Standard Library===
<
open System
open System.IO
Line 558:
File.WriteAllBytes("favicon.ico", decoded)
</syntaxhighlight>
{{out}}
[https://rosettacode.org/favicon.ico Rosetta Code Icon]
===Manual Implementation===
<
open System
open System.IO
Line 590:
File.WriteAllBytes("favicon.ico", decoded)
</syntaxhighlight>
{{out}}
[https://rosettacode.org/favicon.ico Rosetta Code Icon]
=={{header|Factor}}==
<
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo"
base64> >string print</
{{out}}
<pre>
Line 613:
Inspired from Wikipedia. Use of a buffer.
May be also of interest : github.com/lietho/base64-forth
<
: char>6bits ( c -- u )
Line 647:
rot - ( addr2 n2 )
;
</syntaxhighlight>
{{out}}
Line 670:
=={{header|FreeBASIC}}==
<
B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & _
"abcdefghijklmnopqrstuvwxyz" & _
Line 702:
Print msg64
Print: Print(Decode64(msg64))
Sleep</
{{out}}
<pre>
Line 714:
=={{header|Go}}==
As images can no longer be uploaded to RC, I've encoded and decoded a string rather than the Rosetta Code icon.
<
import (
Line 732:
}
fmt.Println("\nDecoded :", string(decoded))
}</
{{out}}
Line 745:
=={{header|Groovy}}==
{{trans|Java}}
<
class Decode {
Line 755:
System.out.println(decodedStr)
}
}</
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 762:
=={{header|Haskell}}==
Simple implementation that decodes an ASCII string.
<
import qualified Data.Map.Strict as Map (Map, lookup, fromList)
import Data.Maybe (fromJust, listToMaybe, mapMaybe)
Line 801:
putStrLn $
byteToASCII
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCi0tIFBhdWwgUi4gRWhybGljaA=="</
{{Out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 810:
or in terms of Data.ByteString.Base64:
<
import qualified Data.ByteString.Base64 as Base64 (decode, encode)
Line 823:
either print B.putStrLn $
Base64.decode
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCi0tIFBhdWwgUi4gRWhybGljaA=="</
{{Out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCi0tIFBhdWwgUi4gRWhybGljaA==
Line 833:
=={{header|Haxe}}==
<
static function main() {
var data = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVw" +
Line 841:
Sys.println(decoded);
}
}</
{{out}}
Line 850:
using J's [https://github.com/jsoftware/convert_misc/blob/master/base64.ijs convert/misc/base64] script:
<
frombase64 'VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g='
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich</
Alternative implementation based on that script:
<syntaxhighlight lang=J>
BASE64=: (a.{~ ,(a.i.'Aa') +/i.26),'0123456789+/'
Line 864:
pad }. a. {~ #. _8 [\ , (6#2) #: BASE64 i. y
}}
</syntaxhighlight>
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.util.Base64;
Line 879:
System.out.println(decodedStr);
}
}</
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 886:
=={{header|JavaScript}}==
=== Browser ===
<
const base64 = 'SGVsbG8sIHdvcmxkIQ==';
// atob is a built-in function.
console.log(atob(base64));</
=== Node.js ===
<
const base64 = Buffer.from('SGVsbG8sIHdvcmxkIQ==', 'base64');
// <Buffer>.toString() is a built-in method.
console.log(base64.toString());</
{{out}}
<pre>Hello, world!</pre>
Line 907:
See [[Base64_encode_data#Jsish]] for ''base64.jsi''.
<
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);</
=={{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.
<
io = IOBuffer()
Line 925:
println(String(read(iob64_decode)))
</
<pre>
To err is human, but to really foul things up you need a computer.
Line 933:
=={{header|Kotlin}}==
{{trans|D}}
<
fun main() {
Line 942:
val decodedStr = String(decoded, Charsets.UTF_8)
println(decodedStr)
}</
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 948:
=={{header|Lua}}==
<
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -- You will need this for encoding/decoding
Line 973:
local decoded = dec(data)
print(decoded)</
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo
Line 981:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo",
"Base64"
]</
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 990:
=={{header|Nim}}==
<
const Source = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="
echo Source.decode()</
{{out}}
Line 1,009:
=={{header|Ol}}==
<
(define base64-codes "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
(define kernel (alist->ff (map cons (string->bytes base64-codes) (iota (string-length base64-codes)))))
Line 1,046:
(decode "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=")
</syntaxhighlight>
{{Out}}
<pre>
Line 1,059:
=={{header|Perl}}==
The MIME::Base64 module is to be preferred, but this works too.
<
my($d) = @_;
$d =~ tr!A-Za-z0-9+/!!cd;
Line 1,078:
EOD
print decode_base64($data) . "\n";</
{{out}}
<pre>'Twas brillig, and the slithy toves
Line 1,086:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
Line 1,093:
<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>
<!--</
{{out}}
<pre>
Line 1,101:
=={{header|PHP}}==
<
'IHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=';
echo
$encoded, PHP_EOL,
base64_decode($encoded), PHP_EOL;</
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 1,112:
=={{header|PicoLisp}}==
<
`'(chop
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ) )
Line 1,135:
(link
(char (| (>> -6 (& C 3)) D)) ) ) ) ) ) ) ) )
(prinl (decode64 "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo"))</
{{out}}
<pre>
Line 1,145:
By necessity this also implements all of the Base64 encode task to
avoid a humongous amount of icon data hardcoded in the program.
<
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>
=={{header|Prolog}}==
Line 1,164:
=={{header|PureBasic}}==
<
"IHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="
Line 1,170:
Base64Decoder(b64cd$, *p_buf, 1024)
OpenConsole("") : PrintN(PeekS(*p_buf, -1, #PB_UTF8))
Input()</
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 1,176:
=={{header|Python}}==
<
import base64
data = 'VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g='
print(base64.b64decode(data).decode('utf-8'))
</syntaxhighlight>
{{out}}
<pre>
Line 1,187:
</pre>
=={{header|QB64}}==
<
Dim As String udata, decoded
Line 1,297:
Wend
decode = sink
End Function</
{{out}}
<pre>
Line 1,309:
{{works with|R|4.1.0}}
{{libheader|base64enc}}
<
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo" |>
base64enc::base64decode() |>
rawToChar() |>
cat()
</
<pre>
To err is human, but to really foul things up you need a computer.
Line 1,324:
{{works with|Rakudo|2018.11}}
<syntaxhighlight lang=raku
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY2
9tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 1,349:
say "\nFast:";
use Base64::Native;
say base64-decode($e64).decode('utf8');</
{{out}}
<pre>Slow:
Line 1,360:
=={{header|Red}}==
<
print to-string debase "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo"
</syntaxhighlight>
{{out}}
<pre>
Line 1,371:
=={{header|Ring}}==
<
#======================================#
# Sample: Base64 decode data
Line 1,387:
oQByteArray.append(oba)
see oQByteArray.fromBase64(oQByteArray).data()
</syntaxhighlight>
{{out}}
<pre>
Line 1,395:
=={{header|Ruby}}==
<
raku_example ='
Line 1,401:
9tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
'
puts Base64.decode64 raku_example</
{{out}}
<pre>
Line 1,409:
=={{header|Rust}}==
<
const INPUT: &str = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo";
Line 1,446:
println!("Output: {}", result);
}</
{{out}}
<pre>
Line 1,456:
=={{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)].
<
object Base64Decode extends App {
Line 1,469:
println(text2BinaryDecoding(data))
}</
=={{header|Seed7}}==
Line 1,476:
[http://seed7.sourceforge.net/libraries/encoding.htm#fromBase64(in_string) fromBase64].
<
include "gethttp.s7i";
include "encoding.s7i";
Line 1,489:
writeln("Is the Rosetta Code icon the same (byte for byte) encoded then decoded: " <&
fromBase64(encoded) = original);
end func;</
{{out}}
Line 1,495:
=={{header|SenseTalk}}==
<
put base64Decode ("VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuIC0tUGF1bCBSLkVocmxpY2g=")
</syntaxhighlight>
Output:
<
=={{header|Sidef}}==
<
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY2
9tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
EOT
say data.decode_base64</
{{out}}
<pre>
Line 1,519:
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY2
9tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
'.
data base64Decoded asString printCR.</
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 1,530:
=={{header|Standard ML}}==
<
let
Line 1,560:
String.implode ( List.map (chr o Word.toInt) ( decodeLst (String.explode input )) )
end handle Subscript => "Invalid code\n" ;</
call
<pre>
Line 1,569:
-- Paul R. Ehrlich </pre>
=={{header|Tcl}}==
<
set data VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
puts [binary decode base64 $data]</
{{out}}
<pre>
Line 1,581:
=={{header|Visual Basic .NET}}==
{{trans|D}}
<
Sub Main()
Line 1,592:
End Sub
End Module</
{{out}}
<pre>VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
Line 1,600:
=={{header|Vlang}}==
<
fn main() {
Line 1,609:
decoded := base64.decode_str(encoded)
println("\nDecoded : $decoded")
}</
{{out}}
Line 1,624:
{{libheader|Wren-str}}
From first principles using string manipulation. Quick enough here.
<
import "/fmt" for Conv, Fmt
import "/str" for Str
Line 1,653:
var s = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="
for (chunk in Str.chunks(s, 4)) decode.call(chunk)
System.print()</
{{out}}
Line 1,663:
=={{header|zkl}}==
Using shared libraries for cURL and message hashing:
<
icon:=Curl().get("http://rosettacode.org/favicon.ico"); //-->(Data(4,331),693,0)
Line 1,671:
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);</
{{out}}
<pre>
Line 1,677:
</pre>
{{out|Text based test}}
<
println("Original: %s\nEncoded: %s\nBytes: %s\nDecoded: %s"
.fmt(msg, b64.text, b64.bytes().apply("toString",16).concat(","),
MsgHash.base64decode(b64).text));</
<pre>
Original: Rosetta Code Base64 decode data task
|