Skip to content

[Rcpp-devel] convert std::string to Rcpp::RawVector

2 messages · Koert Kuipers, Romain Francois

#
Hello all,
I need to convert a std::string to a RAWSXP. I think i know hot do to this
without Rcpp. It goes something like this (i haven't tested this):

SEXP stringToRaw(std::string x) {
  SEXP y;
  PROTECT(y = allocVector(RAWSXP, x.length()));
  memcpy(RAW(y), x.data(), x.length());
  UNPROTECT(1);
  return y;
}

How do i do this with Rcpp? Say i want to return a Rcpp::RawVector?
Thanks,
Koert

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20101007/c5ee9427/attachment.htm>
#
Le 07/10/10 22:04, Koert Kuipers a ?crit :
Hi,

I would litteraly C++ify the code you have to make something like this:


require( inline )
require( Rcpp )

fx <- cxxfunction( , '

     std::string foo( "blabla" ) ;
     RawVector res( foo.size() ) ;
     std::copy( foo.begin(), foo.end(), res.begin() ) ;
     return res ;

', plugin = "Rcpp" )


 > fx( )
[1] 62 6c 61 62 6c 61

 > charToRaw( "blabla" )
[1] 62 6c 61 62 6c 61

Romain