Jump to content

Base64 encode data: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 5:
 
=={{header|ABAP}}==
<syntaxhighlight lang=ABAP"abap">DATA: li_client TYPE REF TO if_http_client,
lv_encoded TYPE string,
lv_data TYPE xstring.
Line 43:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang=Action"action!">INCLUDE "D2:IO.ACT" ;from the Action! Tool Kit
 
PROC Encode(BYTE ARRAY buf BYTE len CHAR ARRAY res)
Line 106:
=={{header|Ada}}==
{{libheader|AWS}}
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO;
 
with AWS.Response;
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.
<syntaxhighlight lang="algol68">
STRING codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[@0];
 
Line 204:
 
=={{header|ARM Assembly}}==
<syntaxhighlight lang=ARM"arm Assemblyassembly">
.section .rodata
ch64: .ascii "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Line 374:
 
=={{header|BaCon}}==
<syntaxhighlight lang="bacon">CONST file$ = "favicon.ico"
binary = BLOAD(file$)
PRINT B64ENC$(binary, FILELEN(file$))
Line 384:
===libresolv===
{{libheader|libresolv}} (libresolv is included on most Unix-like systems)
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <resolv.h>
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.
<syntaxhighlight lang="c">#include <stdio.h>
#include <unistd.h>
 
Line 461:
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">namespace RosettaCode.Base64EncodeData
{
using System;
Line 487:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
#include <iostream>
#include <fstream>
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.
 
<syntaxhighlight lang="basic">
100 print chr$(247);chr$(14);
110 dim a$(63): rem alphabet
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].
<syntaxhighlight lang="lisp">(eval-when (:load-toplevel :compile-toplevel :execute)
(ql:quickload "drakma")
(ql:quickload "cl-base64"))
Line 635:
 
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">
require "http/client"
require "base64"
Line 647:
 
=={{header|D}}==
<syntaxhighlight lang="d">void main() {
import std.stdio, std.base64, std.net.curl, std.string;
 
Line 658:
 
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">program Base64EncodeData;
{$APPTYPE CONSOLE}
uses IdHTTP, IdCoderMIME;
Line 676:
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">data = File.read!("favicon.ico")
encoded = :base64.encode(data)
IO.puts encoded</syntaxhighlight>
Line 688:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">-module(base64demo).
-export([main/0]).
 
Line 706:
===Standard Library===
{{works with|fsharp|4.5}}
<syntaxhighlight lang="fsharp">open System
open System.Net
 
Line 725:
===Manual Implementation===
{{works with|fsharp|4.5}}
<syntaxhighlight lang="fsharp">open System.Net
 
let encode s =
Line 761:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: base64 http.client io kernel strings ;
 
"http://rosettacode.org/favicon.ico" http-get nip
Line 776:
Inspired from Wikipedia. Use of a buffer.
May be also of interest : github.com/lietho/base64-forth
<syntaxhighlight lang="forth">variable bitsbuff
 
: alphabase ( u -- c ) $3F and C" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 1+ + c@ ;
Line 820:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Dim Shared As String B64
B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & _
"abcdefghijklmnopqrstuvwxyz" & _
Line 870:
=={{header|Go}}==
===Standard Library===
<syntaxhighlight lang=Go"go">package main
 
import (
Line 898:
</pre>
===Manual implementation===
<syntaxhighlight lang="go">// base64 encoding
// A port, with slight variations, of the C version found here:
// http://rosettacode.org/wiki/Base64#C (manual implementation)
Line 1,012:
{{Trans|C}}
This Haskell code is ported from the C solution (manual implementation) with slight variations.
<syntaxhighlight lang=Haskell"haskell">-- | Base 64 Encoding.
-- A port, with slight variations, of the C version found here:
-- http://rosettacode.org/wiki/Base64#C (manual implementation)
Line 1,080:
 
===Using Data.ByteString.Base64===
<syntaxhighlight lang="haskell">import qualified Data.ByteString.Base64 as Base64 (decode, encode)
import qualified Data.ByteString.Char8 as B (putStrLn, readFile)
 
Line 1,087:
 
=={{header|J}}==
'''Solution''' (''[http://www.jsoftware.com/wsvn/addons/trunk/convert/misc/base64.ijs standard library]''):<syntaxhighlight lang="j"> load'convert/misc/base64' NB. use 'tobase64'</syntaxhighlight>
'''Solution''' (''handrolled''):<syntaxhighlight lang="j"> tobase64 =: padB64~ b2B64
padB64 =: , '=' #~ 0 2 1 i. 3 | #
b2B64 =: BASE64 {~ _6 #.\ (8#2) ,@:#: a.&i.</syntaxhighlight>
'''Example''':<syntaxhighlight lang="j"> load'web/gethttp'
76 {. tobase64 gethttp 'http://rosettacode.org/favicon.ico'
AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAEAAAACAAAAABAAgAAAAA</syntaxhighlight>
Line 1,099:
Can also use org.apache.commons.codec.binary.Base64 from Apache Commons Codec
 
<syntaxhighlight lang=Java"java">import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Line 1,173:
 
=== Java 8 version ===
<syntaxhighlight lang="java">import java.nio.file.*;
import java.util.Base64;
 
Line 1,188:
 
=={{header|JavaScript}}==
<syntaxhighlight lang=JavaScript"javascript">(function(){//ECMAScript doesn't have an internal base64 function or method, so we have to do it ourselves, isn't that exciting?
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,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]
<syntaxhighlight lang=JavaScript"javascript">window.btoa("String to encode, etc..");//Will throw error if any unicode character is larger than 255 it's counterpart it's the window.atob</syntaxhighlight>To make it.. just work, you could convert it to UTF-8 Manually or..
JSON.stringify it or..
encodeURIComponent it.
Line 1,266:
===Using Node.js===
{{works with|Node.js}}
<syntaxhighlight lang=JavaScript"javascript">var http = require('http');
var options = {
host: 'rosettacode.org',
Line 1,286:
<nowiki>https://jsish.org/fossil/jsi/wiki/Wget</nowiki> and also listed at [[HTTP#Jsish]].
 
<syntaxhighlight lang="javascript">/* Base64, in Jsish */
require('httpGet');
var icon = httpGet('http://rosettacode.org/favicon.ico');
Line 1,311:
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">using Requests
 
file = read(get("https://rosettacode.org/favicon.ico"))
Line 1,322:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.2
 
import java.io.File
Line 1,340:
 
=={{header|Lasso}}==
<syntaxhighlight lang=Lasso"lasso ">local(
src = curl('http://rosettacode.org/favicon.ico'),
srcdata = #src->result
Line 1,350:
 
=={{header|LiveCode}}==
<syntaxhighlight lang=LiveCode"livecode">put URL "http://rosettacode.org/favicon.ico" into rosettaico
put base64encode(rosettaico)
 
Line 1,357:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
local dic = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
function encode( t, f )
Line 1,400:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">ExportString[Import["http://rosettacode.org/favicon.ico", "Text"], "Base64"]</syntaxhighlight>
Very interesting results.
{{out}}
Line 1,412:
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim"nim">import base64
import httpclient
 
Line 1,430:
{{works with|Mac OS X|10.6+}}
{{works with|iOS|4.0+}}
<syntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main(int argc, const char *argv[]) {
Line 1,469:
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
(define base64-codes "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
(define kernel (alist->ff (map cons (iota (string-length base64-codes)) (string->bytes base64-codes))))
Line 1,527:
 
The rosettacode icon:
<syntaxhighlight lang="scheme">
(define icon (runes->string (bytevector->list (file->bytevector "favicon.ico"))))
(encode icon)
Line 1,538:
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!perl
use strict;
use warnings;
Line 1,554:
=={{header|Phix}}==
For simplicity, the example from wp:
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<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,576:
This downloads, encodes, decodes, and verifies the icon:
{{libheader|Phix/libcurl}}
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<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,606:
 
=={{header|PHP}}==
<syntaxhighlight lang="php"><?php echo base64_encode(file_get_contents("http://rosettacode.org/favicon.ico"));/*1 liner*/ ?></syntaxhighlight>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">`(== 64 64)
(setq *Char64
`'(chop
Line 1,656:
 
=={{header|Pike}}==
<syntaxhighlight lang=Pike"pike">
string icon = Protocols.HTTP.get_url_data("http://rosettacode.org/favicon.ico");
// The default base64 encodeing linefeeds every 76 chars
Line 1,671:
 
=={{header|PowerShell}}==
<syntaxhighlight lang=PowerShell"powershell">$webClient = [Net.WebClient]::new()
$bytes = $webClient.DownloadData('http://rosettacode.org/favicon.ico')
Line 1,683:
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">InitNetwork()
*BufferRaw = ReceiveHTTPMemory("http://rosettacode.org/favicon.ico")
Line 1,693:
 
=={{header|Python}}==
<syntaxhighlight lang="python">import urllib
import base64
 
Line 1,701:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
(require net/url net/base64)
Line 1,708:
</syntaxhighlight>
Output:
<syntaxhighlight lang="racket">
#"AAABAAIAEBAAAAAAAABoBQAA...AQAAAAE=\r\n"
</syntaxhighlight>
Line 1,714:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub MAIN {
my $buf = slurp("./favicon.ico", :bin);
say buf-to-Base64($buf);
Line 1,741:
 
=={{header|Red}}==
<syntaxhighlight lang="red">Red [Source: https://github.com/vazub/rosetta-red]
 
print enbase read/binary https://rosettacode.org/favicon.ico
Line 1,755:
 
A much higher value for &nbsp; '''chunk''' &nbsp; could be used for modern systems or implementations.
<syntaxhighlight lang="rexx">/*REXX program converts text (from a file or the C.L.) to a base64 text string. */
parse arg iFID @ /*obtain optional arguments from the CL*/
if iFID=='' | iFID=="," then iFID='favicon.ico' /*Not specified? Then use the default.*/
Line 1,824:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
#=======================================#
# Description : Base64 Sample
Line 1,859:
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'open-uri'
require 'base64'
 
Line 1,865:
 
=={{header|Scala}}==
<syntaxhighlight lang=Scala"scala">import java.net.URL
import java.util.Base64
 
Line 1,885:
which encodes a string with the Base64 encoding.
 
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "gethttp.s7i";
include "encoding.s7i";
Line 1,895:
 
=={{header|SenseTalk}}==
<syntaxhighlight lang="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:
<syntaxhighlight lang="sensetalk">
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVk
IGEgY29tcHV0ZXIuIC0tUGF1bCBSLkVocmxpY2g=
Line 1,909:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var data = %f'favicon.ico'.read(:raw) # binary string
print data.encode_base64 # print to STDOUT</syntaxhighlight>
 
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<syntaxhighlight lang="tcl">package require Tcl 8.6
package require http
 
Line 1,924:
With older versions of Tcl, the base64 encoding is best supported via an external package:
{{tcllib|base64}}
<syntaxhighlight lang="tcl">package require base64
package require http
 
Line 1,934:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Option Explicit
Public Function Decode(s As String) As String
Dim i As Integer, j As Integer, r As Byte
Line 2,054:
{{libheader|Wren-seq}}
From first principles using string manipulation. Quick enough here.
<syntaxhighlight lang="ecmascript">import "io" for File, Stdout
import "/fmt" for Conv, Fmt
import "/seq" for Lst
Line 2,097:
=={{header|zkl}}==
Using shared libraries for cURL and message hashing:
<syntaxhighlight lang="zkl">var [const] MsgHash=Import("zklMsgHash"), Curl=Import("zklCurl");
icon:=Curl().get("http://rosettacode.org/favicon.ico"); //-->(Data(4,331),693,0)
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.