[Rcpp-devel] String encoding (UTF-8 conversion)

Jeroen Ooms jeroen.ooms at stat.ucla.edu
Fri Dec 12 00:23:58 CET 2014


On Thu, Dec 11, 2014 at 2:16 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
> Maybe the same way?  ;-)

OK I am not completely back at the C api, and it still doesn't work
when called via Rcpp:

// [[Rcpp::export]]
SEXP echo(SEXP src){
  const char* utf8str = Rf_translateCharUTF8(Rf_asChar(src));
  SEXP out = PROTECT(Rf_allocVector(STRSXP, 1));
  SET_STRING_ELT(out, 0, Rf_mkCharCE(utf8str, CE_UTF8));
  UNPROTECT(1);
  return out;
}

> test
[1] "東京"
> echo(test)
[1] "æ ±äº¬"
> echo(enc2utf8 (test))
[1] "æ ±äº¬"

Also:

> echo("Zürich")
[1] "Zürich"
> echo(enc2utf8("Zürich"))
[1] "Zürich"

The same function works perfectly fine when invoked via .Call(). Does
Rcpp somehow override the CE or attempt to recode strings before
giving them back to R?


More information about the Rcpp-devel mailing list