Thanks for your reply! Sorry I did not clarify that I was using base64encode and base64decode functions provide from "caTools" package. It seems that if I convert the string to the raw type first, it still solves my problem.
My original problem actually is that I have a string: secret <- '5Kwug+Byrq+ilULMz3IBD5tquNt5CcdYi3XPc8jnKwtXvIgHw/vcSGU1VCIo4b/OfcRDm7uH359syfhWzXFrNg==' It was claimed to be encoded in Base64. So I tried to decode it: require("base64enc") rawToChar(base64decode(secret)) Then, I got "\xe4\xac.\x83\xe0r\xae\xaf\xa2\x95B\xcc\xcfr\001\017\x9bj\xb8\xdby\t\xc7X\x8bu\xcfs\xc8\xe7+\vW\xbc\x88\a\xc3\xfb\xdcHe5T\"(\xe1\xbf\xce}\xc4C\x9b\xbb\x87ßl\xc9\xf8V\xcdqk6" But what I suppose to get is: '\xe4\xac.\x83\xe0r\xae\xaf\xa2\x95B\xcc\xcfr\x01\x0f\x9bj\xb8\xdby\t\xc7X\x8bu\xcfs\xc8\xe7+\x0bW\xbc\x88\x07\xc3\xfb\xdcHe5T"(\xe1\xbf\xce}\xc4C\x9b\xbb\x87\xdf\x9fl\xc9\xf8V\xcdqk6' Most part of the result is correct except several characters near the end. I don't know where the problem is. On Sat, Aug 3, 2013 at 3:49 PM, Enrico Schumann <e...@enricoschumann.net>wrote: > On Fri, 02 Aug 2013, Qiang Wang <uns...@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 > [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.