Learn R Programming

caTools (version 1.2)

base64: Convert R vectors to/from the Base64 format

Description

Convert R vectors of any type to and from the Base64 format for encrypting any binary data as string using alphanumeric subset of ASCII character set.

Usage

z = base64encode(x, ...)
  x = base64decode(z, what, ...)

Arguments

x
vector or any structure that can be converted to a vector by as.vector function. Strings are also allowed.
z
String with Base64 code, using [A-Z,a-z,0-9,+,/,=] subset of characters
what
Either an object whose mode will give the mode of the vector to be created, or a character vector of length one describing the mode: one of '"numeric", "double", "integer", "int", "logical", "complex", "character", "raw".
...
parameters to be passed to bin2raw and raw2bin functions.

Value

  • Function base64encode returns a string with Base64 code. Function base64decode returns vector of appropriate mode and length (see x above).

Details

The Base64 encoding is designed to encode arbitrary binary information for transmission by electronic mail. It is defined by MIME (Multipurpose Internet Mail Extensions) specification RFC 1341, RFC 1421, RFC 2045 and others. Triplets of 8-bit octets are encoded as groups of four characters, each representing 6 bits of the source 24 bits. Only a 65-character subset ([A-Z,a-z,0-9,+,/,=]) present in all variants of ASCII and EBCDIC is used, enabling 6 bits to be represented per printable character

References

  • Base64 description inConnected: An Internet Encyclopediahttp://www.freesoft.org/CIE/RFC/1521/7.htm
  • MIME RFC 1341http://www.faqs.org/rfcs/rfc1341.html
  • MIME RFC 1421http://www.faqs.org/rfcs/rfc1421.html
  • MIME RFC 2045http://www.faqs.org/rfcs/rfc2045.html
  • Portions of the code are based on Matlab code by Peter Acklamhttp://home.online.no/~pjacklam/matlab/software/util/datautil/

See Also

  • bin2rawandraw2binare being used to convert R vectors to and from the raw binary format.
  • xmlValuefromXMLpackage often reads XML code which sometimes is encoded in Base64 format.

Examples

Run this code
x = (10*runif(10)>5) # logical
   for (i in c(NA, 1, 2, 4)) {
     y = base64encode(x, size=i)
     z = base64decode(y,typeof(x), size=i)
     stopifnot(x==z)
   }
   print("Checked base64 for encode/decode logical type")

   x = as.integer(1:10) # integer
   for (i in c(NA, 1, 2, 4)) {
     y = base64encode(x, size=i)
     z = base64decode(y,typeof(x), size=i)
     stopifnot(x==z)
   }
   print("Checked base64 encode/decode for integer type")
   
   x = (1:10)*pi        # double
   for (i in c(NA, 4, 8)) {
     y = base64encode(x, size=i)
     z = base64decode(y,typeof(x), size=i)
     stopifnot(mean(abs(x-z))<1e-5)
   }
   print("Checked base64 for encode/decode double type")
   
   x = log(as.complex(-(1:10)*pi))        # complex
   y = base64encode(x)
   z = base64decode(y,typeof(x))
   stopifnot(x==z)
   print("Checked base64 for encode/decode complex type")
  
   x = "Chance favors the prepared mind" # character
   y = base64encode(x)
   z = base64decode(y,typeof(x))
   stopifnot(x==z)
   print("Checked base64 for encode/decode character type")

Run the code above in your browser using DataLab