From whorfin at pixar.com  Mon Jun 15 23:45:09 2009
From: whorfin at pixar.com (whorfin at pixar.com)
Date: Mon, 15 Jun 2009 23:45:09 +0200 (CEST)
Subject: [Rd] Wishlist: as.raw [or similar] support for tcl strings
	(PR#13758)
Message-ID: <20090615214509.41609282EFF0@mail.pubhealth.ku.dk>

Full_Name: Rick Sayre
Version: 2.9.0
OS: windows, os x, linux
Submission from: (NULL) (138.72.153.236)


if a tcl "string" contains binary data, there is a problem getting this data
to R, since there appears to be no way to get "raw" data without NULL
interpretation.  Here is a simple example.  The data should be
Here are 1<one NULL>Here is 2<two NULLs>And finally three<three NULLS>
> tq <- .Tcl("set tcls [binary format H*
\"48657265206172652031004865726520697320320000416E642066696E616C6C79207468726565000000\"]")
#tclvalue stops returning data after the first NULL
> tclvalue("tcls")
[1] "Here are 1"
#interestingly, as.character gets more, but gets confused
> as.character(tq)
[1] "Here"    "are"     "1"       "is"      "2"       "finally" "three"
> # we confirm that the tcl representation does indeed have the NULLs:
> .Tcl("binary scan $tcls H* tmps")
<Tcl> 1
> tclvalue("tmps")
[1] "48657265206172652031004865726520697320320000416e642066696e616c6c79207468726565000000"

I would naively suggest something like tclrawvalue() and/or as.raw() utilizing
Tcl_GetByteArrayFromObj

A good discussion of the basics of this issue for general tcl embedding can be
found here:
http://www2.tcl.tk/1180

For those who might be wondering "what the heck are you doing trying to pass
binary data from tcl?", the answer is that non-blocking I/O does not work on
windows,
and timeouts, modes, and line control for RS232 ports aren't supported at all on
R.
Using tcl provides a nice portable way to do this in R, but currently requires
converting everything to ascii encodings since the binary data fails to make it
across.

Cheers
   --Rick



[Rd] Wishlist: as.raw [or similar] support for tcl strings (PR#13758)
whorfin at pixar.com