Base64 encode data: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 5:
=={{header|ABAP}}==
<
lv_encoded TYPE string,
lv_data TYPE xstring.
Line 32:
ENDWHILE.
WRITE: / lv_encoded.
</syntaxhighlight>
{{out}}
Line 43:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<
PROC Encode(BYTE ARRAY buf BYTE len CHAR ARRAY res)
Line 95:
PROC Main()
EncodeFile("H1:FAVICON.ICO")
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Base64_encode_data.png Screenshot from Atari 8-bit computer]
Line 106:
=={{header|Ada}}==
{{libheader|AWS}}
<
with AWS.Response;
Line 119:
begin
Ada.Text_IO.Put_Line (Icon_64);
end Encode_AWS;</
{{out}}
Line 130:
=={{header|ALGOL 68}}==
This program is run on a modified Algol 68 Genie 2.8. That interpreter has some bugs, so it does not do binary tcp/ip requests, and I made patches/bugfixes to it in order to run this task.
<
STRING codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[@0];
Line 194:
STRING encoded icon = base64_encode (rosettacode icon);
print ((encoded icon, new line))
</syntaxhighlight>
{{out}}
<pre>
Line 204:
=={{header|ARM Assembly}}==
<
.section .rodata
ch64: .ascii "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Line 371:
mov r0, #0
swi #0
</syntaxhighlight>
=={{header|BaCon}}==
<
binary = BLOAD(file$)
PRINT B64ENC$(binary, FILELEN(file$))
FREE binary</
{{out}}
<pre>AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAE.......QAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAE=</pre>
Line 384:
===libresolv===
{{libheader|libresolv}} (libresolv is included on most Unix-like systems)
<
#include <stdlib.h>
#include <resolv.h>
Line 422:
return 0;
}</
Compile with
<pre>gcc -lresolv -o base64encode base64encode.c</pre>
Line 428:
===Manual implementation===
The following reads standard input and writes base64-encoded stream to standard output, e.g. <tt>./a.out <some_random_file >/dev/null</tt> if you don't want to see the output. It gives identical output as the common <tt>base64</tt> utility program, though much less efficiently.
<
#include <unistd.h>
Line 458:
return 0;
}</
=={{header|C sharp|C#}}==
<
{
using System;
Line 482:
}
}
}</
Output:
<pre>AAABAAIAEBAAAAAAAABoBQAAJgAAACAg...AAABAAAAAQAAAAEAAAABAAAAAQAAAAE=</pre>
=={{header|C++}}==
<
#include <iostream>
#include <fstream>
Line 544:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 557:
Assumes the source file is a PRG on disk drive device 8, writes encoded file both to a SEQ file on the same disk and to the screen (where it looks good in 80-column mode on a PET or C128, a little less so on a C64 or VIC). This is all done in PETSCII; transfer out of Commodore-land will require translation of the Base64-encoded file into ASCII.
<
100 print chr$(247);chr$(14);
110 dim a$(63): rem alphabet
Line 595:
450 print "error:"st
460 open 15,8,15:input#15,ds,ds$,a,b:close15
470 print ds,ds$,a,b</
{{Out}}
Line 609:
A nice example for the CL eco system using [http://quickdocs.org/cl-base64/ cl-base64] and [https://www.cliki.net/Drakma drakma].
<
(ql:quickload "drakma")
(ql:quickload "cl-base64"))
Line 630:
finally (return (usb8-array-to-base64-string array)))))
(close input)
output))</
{{out}}
<pre>"AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAEAAAACAAAAABAAgAAAAAAEABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP///...</pre>
=={{header|Crystal}}==
<
require "http/client"
require "base64"
Line 643:
Base64.encode(response.body, STDOUT)
end
</syntaxhighlight>
=={{header|D}}==
<
import std.stdio, std.base64, std.net.curl, std.string;
const f = "http://rosettacode.org/favicon.ico".get.representation;
Base64.encode(f).writeln;
}</
{{out}}
<pre>AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAwqgIAADCjgUAACgAAAAQAAAAIAA...
Line 658:
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
uses IdHTTP, IdCoderMIME;
Line 673:
lHTTP.Free;
end;
end.</
=={{header|Elixir}}==
<
encoded = :base64.encode(data)
IO.puts encoded</
{{out}}
Line 688:
=={{header|Erlang}}==
<
-export([main/0]).
Line 698:
%% Demonstrating with the library function.
encode_library(Data) ->
base64:encode(Data).</
{{out}}
Line 706:
===Standard Library===
{{works with|fsharp|4.5}}
<
open System.Net
Line 718:
let encoded = Convert.ToBase64String raw
printfn "%s" encoded</
{{out}}
Line 725:
===Manual Implementation===
{{works with|fsharp|4.5}}
<
let encode s =
Line 755:
printfn "%s" encoded
</syntaxhighlight>
{{out}}
Line 761:
=={{header|Factor}}==
<
"http://rosettacode.org/favicon.ico" http-get nip
>base64-lines >string print</
{{out}}
<pre>
Line 776:
Inspired from Wikipedia. Use of a buffer.
May be also of interest : github.com/lietho/base64-forth
<
: alphabase ( u -- c ) $3F and C" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 1+ + c@ ;
Line 805:
dup here swap - ( addr2 n2 )
;
</syntaxhighlight>
{{out}}
Line 820:
=={{header|FreeBASIC}}==
<
B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & _
"abcdefghijklmnopqrstuvwxyz" & _
Line 860:
Print msg64
Print: Print(Encode64(msg64))
Sleep</
{{out}}
<pre>To err is human, but to really foul things up you need a computer.
Line 870:
=={{header|Go}}==
===Standard Library===
<
import (
Line 892:
}
fmt.Println(base64.StdEncoding.EncodeToString(d))
}</
{{out}}
<pre>
Line 898:
</pre>
===Manual implementation===
<
// A port, with slight variations, of the C version found here:
// http://rosettacode.org/wiki/Base64#C (manual implementation)
Line 998:
}
fmt.Printf("%s", encoded)
}</
{{out}}
<pre>
Line 1,012:
{{Trans|C}}
This Haskell code is ported from the C solution (manual implementation) with slight variations.
<
-- A port, with slight variations, of the C version found here:
-- http://rosettacode.org/wiki/Base64#C (manual implementation)
Line 1,077:
main :: IO ()
main = C.getContents >>= C.putStr . b64EncodePretty</
===Using Data.ByteString.Base64===
<
import qualified Data.ByteString.Char8 as B (putStrLn, readFile)
main :: IO ()
main = B.readFile "favicon.ico" >>= (B.putStrLn . Base64.encode)</
=={{header|J}}==
'''Solution''' (''[http://www.jsoftware.com/wsvn/addons/trunk/convert/misc/base64.ijs standard library]''):<
'''Solution''' (''handrolled''):<
padB64 =: , '=' #~ 0 2 1 i. 3 | #
b2B64 =: BASE64 {~ _6 #.\ (8#2) ,@:#: a.&i.</
'''Example''':<
76 {. tobase64 gethttp 'http://rosettacode.org/favicon.ico'
AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAEAAAACAAAAABAAgAAAAA</
=={{header|Java}}==
Line 1,099:
Can also use org.apache.commons.codec.binary.Base64 from Apache Commons Codec
<
import java.io.IOException;
import java.io.InputStream;
Line 1,169:
}
}
}</
<pre>AAABAAIAEBAAAAAAAABoBQ...QAAAAEAAAABAAAAAQAAAAE=</pre>
=== Java 8 version ===
<
import java.util.Base64;
Line 1,183:
System.out.println(result);
}
}</
<pre>AAABAAIAEBAAAAAAAABoBQ...QAAAAEAAAABAAAAAQAAAAE=</pre>
=={{header|JavaScript}}==
<
function stringToArrayUnicode(str){for(var i=0,l=str.length,n=[];i<l;i++)n.push(str.charCodeAt(i));return n;}
function generateOnesByLength(n){//Attempts to generate a binary number full of ones given a length.. they don't redefine each other that much.
Line 1,252:
return toBase64(stringToArrayUnicode("Nothing seems hard to the people who don't know what they're talking about."))
}())</
===Using btoa (HTML5)===
Line 1,260:
HTML5 saves the day! introducing two methods to the DOM!
These are btoa and atob, see [http://dev.w3.org/html5/spec-LC/webappapis.html#atob spec]
<
JSON.stringify it or..
encodeURIComponent it.
Line 1,266:
===Using Node.js===
{{works with|Node.js}}
<
var options = {
host: 'rosettacode.org',
Line 1,280:
});
}
</syntaxhighlight>
=={{header|Jsish}}==
Line 1,286:
<nowiki>https://jsish.org/fossil/jsi/wiki/Wget</nowiki> and also listed at [[HTTP#Jsish]].
<
require('httpGet');
var icon = httpGet('http://rosettacode.org/favicon.ico');
printf("%s", Util.base64(icon, false))</
{{out}}
Line 1,311:
{{works with|Julia|0.6}}
<
file = read(get("https://rosettacode.org/favicon.ico"))
encoded = base64encode(file)
print(encoded)</
{{out}}
Line 1,322:
=={{header|Kotlin}}==
<
import java.io.File
Line 1,332:
val base64 = Base64.getEncoder().encodeToString(bytes)
println(base64)
}</
{{out}}
Line 1,340:
=={{header|Lasso}}==
<
src = curl('http://rosettacode.org/favicon.ico'),
srcdata = #src->result
Line 1,347:
// or, in one movement:
curl('http://rosettacode.org/favicon.ico')->result->encodebase64</
=={{header|LiveCode}}==
<
put base64encode(rosettaico)
Ouput
AAABAA...S0tLS0tLS0t...QAAAAE=</
=={{header|Lua}}==
<
local dic = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
function encode( t, f )
Line 1,389:
end
print()
</syntaxhighlight>
{{out}}
<pre>
Line 1,400:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Very interesting results.
{{out}}
Line 1,412:
=={{header|Nim}}==
<
import httpclient
Line 1,422:
echo encoded
else:
echo encoded[0..31] & "..." & encoded[^32..^1]</
{{out}}
Line 1,430:
{{works with|Mac OS X|10.6+}}
{{works with|iOS|4.0+}}
<
int main(int argc, const char *argv[]) {
Line 1,438:
}
return 0;
}</
=={{header|OCaml}}==
Line 1,469:
=={{header|Ol}}==
<
(define base64-codes "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
(define kernel (alist->ff (map cons (iota (string-length base64-codes)) (string->bytes base64-codes))))
Line 1,518:
(encode "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.")
</syntaxhighlight>
{{Out}}
<pre>
Line 1,527:
The rosettacode icon:
<
(define icon (runes->string (bytevector->list (file->bytevector "favicon.ico"))))
(encode icon)
</syntaxhighlight>
<pre>
AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAEAAAACAAAAABAAgAAAAAAEABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP///wCG
Line 1,538:
=={{header|Perl}}==
<
use strict;
use warnings;
Line 1,545:
local $/;
print encode_base64(<$fh>);
</syntaxhighlight>
{{out}}
Line 1,554:
=={{header|Phix}}==
For simplicity, the example from wp:
<!--<
<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,565:
<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,576:
This downloads, encodes, decodes, and verifies the icon:
{{libheader|Phix/libcurl}}
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">url</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"https://rosettacode.org/favicon.ico"</span><span style="color: #0000FF;">,</span>
Line 1,596:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"base 64: %s, same: %t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b64</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"chars"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">chk</span><span style="color: #0000FF;">==</span><span style="color: #000000;">raw</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 1,606:
=={{header|PHP}}==
<
=={{header|PicoLisp}}==
<
(setq *Char64
`'(chop
Line 1,653:
(test
"c3VyZS4="
(base64 "sure.") )</
=={{header|Pike}}==
<
string icon = Protocols.HTTP.get_url_data("http://rosettacode.org/favicon.ico");
// The default base64 encodeing linefeeds every 76 chars
Line 1,662:
// For brivety, just print the first and last line
write("%s\n...\n%s\n", encoded_lines[0], encoded_lines[-1]);
</syntaxhighlight>
{{Out}}
<pre>
Line 1,671:
=={{header|PowerShell}}==
<
$bytes = $webClient.DownloadData('http://rosettacode.org/favicon.ico')
$output = [Convert]::ToBase64String($bytes)
$output</
{{out}}
<pre>
Line 1,683:
=={{header|PureBasic}}==
<
*BufferRaw = ReceiveHTTPMemory("http://rosettacode.org/favicon.ico")
Line 1,690:
Else
Debug "Download failed"
EndIf</
=={{header|Python}}==
<
import base64
data = urllib.urlopen('http://rosettacode.org/favicon.ico').read()
print base64.b64encode(data)</
(For me this gets the wrong data; the data is actually an error message. But still, it base-64 encodes it.)
=={{header|Racket}}==
<
#lang racket
(require net/url net/base64)
(base64-encode (call/input-url (string->url "http://rosettacode.org/favicon.ico")
get-pure-port port->bytes))
</syntaxhighlight>
Output:
<
#"AAABAAIAEBAAAAAAAABoBQAA...AQAAAAE=\r\n"
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang=raku
my $buf = slurp("./favicon.ico", :bin);
say buf-to-Base64($buf);
Line 1,736:
else { take '==' }
}
}</
{{out}}
<pre>AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAA...QAAAAEAAAABAAAAAQAAAAE=</pre>
=={{header|Red}}==
<
print enbase read/binary https://rosettacode.org/favicon.ico
</syntaxhighlight>
{{out}}
<pre>
Line 1,755:
A much higher value for '''chunk''' could be used for modern systems or implementations.
<
parse arg iFID @ /*obtain optional arguments from the CL*/
if iFID=='' | iFID=="," then iFID='favicon.ico' /*Not specified? Then use the default.*/
Line 1,784:
end /*j*/
/* [↓] maybe append equal signs to $. */
return $ || copies('=', 2 * (L//6==2) + (L//6==4) )</
For the various outputs, several input texts from the Wikipedia article on ''Base64'' [http://en.wikipedia.org/wiki/Base64] were used to demonstrate how padding works.
<br><br>
Line 1,824:
=={{header|Ring}}==
<
#=======================================#
# Description : Base64 Sample
Line 1,851:
oQByteArray.append("bXkgc3RyaW5n")
? oQByteArray.fromBase64(oQByteArray).data()
</syntaxhighlight>
{{out}}
<pre>
Line 1,859:
=={{header|Ruby}}==
<
require 'base64'
puts Base64.encode64 open('http://rosettacode.org/favicon.ico') {|f| f.read}</
=={{header|Scala}}==
<
import java.util.Base64
Line 1,878:
println(s"Successfully completed without errors. [total ${compat.Platform.currentTime - executionStart} ms]")
}</
=={{header|Seed7}}==
Line 1,885:
which encodes a string with the Base64 encoding.
<
include "gethttp.s7i";
include "encoding.s7i";
Line 1,892:
begin
writeln(toBase64(getHttp("rosettacode.org/favicon.ico")));
end func;</
=={{header|SenseTalk}}==
<
put "To err is human, but to really foul things up you need a computer. --Paul R.Ehrlich" as base64
put base64Encode ("To err is human, but to really foul things up you need a computer. --Paul R.Ehrlich")
</syntaxhighlight>
Output:
<
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVk
IGEgY29tcHV0ZXIuIC0tUGF1bCBSLkVocmxpY2g=
Line 1,906:
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVk
IGEgY29tcHV0ZXIuIC0tUGF1bCBSLkVocmxpY2g=
</syntaxhighlight>
=={{header|Sidef}}==
<
print data.encode_base64 # print to STDOUT</
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
package require http
Line 1,921:
http::cleanup $tok
puts [binary encode base64 -maxlen 64 $icondata]</
With older versions of Tcl, the base64 encoding is best supported via an external package:
{{tcllib|base64}}
<
package require http
Line 1,931:
http::cleanup $tok
puts [base64::encode -maxlen 64 $icondata]</
=={{header|VBA}}==
<
Public Function Decode(s As String) As String
Dim i As Integer, j As Integer, r As Byte
Line 2,042:
Debug.Print "Result of string comparison of input and decoded output: " & StrComp(In_, bIn, vbBinaryCompare)
Debug.Print "A zero indicates both strings are equal."
End Sub</
{{out}}<pre>The first eighty and last eighty characters after encoding:
AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAEAAAACAAAAABAAgAAAAAAEAB
Line 2,054:
{{libheader|Wren-seq}}
From first principles using string manipulation. Quick enough here.
<
import "/fmt" for Conv, Fmt
import "/seq" for Lst
Line 2,086:
var s = File.read("favicon.ico").bytes.toList
for (chunk in Lst.chunks(s, 3)) encode.call(chunk)
System.print()</
{{out}}
Line 2,097:
=={{header|zkl}}==
Using shared libraries for cURL and message hashing:
<
icon:=Curl().get("http://rosettacode.org/favicon.ico"); //-->(Data(4,331),693,0)
Line 2,105:
icon==MsgHash.base64decode(b64));
b64.println();
b64.text.println();</
{{out}}
Encoded to 72 characters per line
|