Encodes / decodes a character string or arbitrary R object to base64 encoding.

base64enc(x, convert = TRUE)

base64dec(x, convert = TRUE)

Arguments

x

an object.

convert

For base64enc: [default TRUE] logical TRUE to encode to a character string or FALSE to a raw vector.
For base64dec: [default TRUE] logical TRUE to convert back to a character string, FALSE to convert back to a raw vector or NA to decode and then unserialize back to the original object.

Value

For base64enc: A character string or raw vector depending on the value of 'convert'.

For base64dec: A character string, raw vector, or other object depending on the value of 'convert'.

Details

For encoding: a character string or raw vector (with no attributes) is encoded 'as is', whilst all other objects are first serialized (using R serialisation version 3, big-endian representation).

For decoding: the value of 'convert' should be set to TRUE, FALSE or NA to be the analogue of the above 3 cases in order to return the original object.

Examples

base64enc("hello world!")
#> [1] "aGVsbG8gd29ybGQh"
base64dec(base64enc("hello world!"))
#> [1] "hello world!"

base64enc(as.raw(c(1L, 2L, 4L)), convert = FALSE)
#> [1] 41 51 49 45
base64dec(base64enc(as.raw(c(1L, 2L, 4L))), convert = FALSE)
#> [1] 01 02 04

base64enc(data.frame())
#> [1] "WAoAAAADAAQDAwADBQAAAAAFVVRGLTgAAAMTAAAAAAAABAIAAAABAAQACQAAAAVuYW1lcwAAABAAAAAAAAAEAgAAAAEABAAJAAAACXJvdy5uYW1lcwAAAA0AAAAAAAAEAgAAAAEABAAJAAAABWNsYXNzAAAAEAAAAAEABAAJAAAACmRhdGEuZnJhbWUAAAD+"
base64dec(base64enc(data.frame()), convert = NA)
#> data frame with 0 columns and 0 rows