[Rd] wchar and wstring.

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Aug 27 13:33:15 CEST 2005

On Fri, 26 Aug 2005, James Bullard wrote:

> Hello all, I am writing an R interface to some C++ files which make use
> of std::wstring classes for internationalization. Previously (when I
> wanted to make R strings from C++ std::strings), I would do something
> like this to construct a string in R from the results of the parse.
> SET_VECTOR_ELT(vals, i++, mkString(header.GetHeader().c_str()));

That creates a list of one-element character vectors.  It would be more
usual to do

   SET_STRING_ELT(vals, i++, mkChar(header.GetHeader().c_str()));

> However, now the call header.GetHeader().c_str() returns a pointer to an 
> array of wchar_t's. I was going to use wcstombs() to convert the 
> wchar_t* to char*, but I wanted to see if there was a similar function 
> in R for the mkString function which I had initially used which deals 
> with wchar_ts as opposed to chars.

No (nor an analogue of mkChar).  R uses MBCS and not wchar_t internally 
(and Unix-alike systems do externally).  There is no wchar_t internal R 
type (a much-debated design decision at the time).

> Also, since I have no experience with the wctombs() function I wanted to 
> ask if anyone knew if this will handle the internationilzation issues 
> from within R.

Did you mean wcstombs or wctomb (if the latter, wcrtomb is preferred)?
There are tens of examples in the R sources for you to consult.

Note that not all R platforms support wchar_t, hence this code is 
surrounded by #ifdef SUPPORT_MBCS macros (exported in Rconfig.h for 
package writers).

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-devel mailing list