[R] cannot base64decode string which is base64encode in R

Enrico Schumann es at enricoschumann.net
Sat Aug 3 09:49:17 CEST 2013


On Fri, 02 Aug 2013, Qiang Wang <unsown at gmail.com> writes:

> Hi,
>
> I'm struggling with encode/decode strings in R. Don't know why the second
> example below would fail. Thanks in advance for your help.
> succeed: s <- "saf" x <- base64encode(s) y <- base64decode(x, "character")
> fail: s <- "safs" x <- base64encode(s) y <- base64decode(x, "character")
>

And the first example works for you?

  require("base64enc")
  s <- "saf"
  x <- base64encode(s)

## Error in file(what, "rb") : cannot open the connection
## In addition: Warning message:
## In file(what, "rb") : cannot open file 'saf': No such file or directory

?base64encode says that its first argument is

    "data to be encoded/decoded. For ‘base64encode’ it can be a raw
     vector, text connection or file name. For ‘base64decode’ it can be
     a string or a binary connection."

Try this:

  rawToChar(base64decode(base64encode(charToRaw("saf"))))

## [1] "saf"

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-help mailing list