0
|
1 \name{packing}
|
|
2 \alias{.pack}
|
|
3 \alias{packing}
|
|
4 \alias{.unpack}
|
|
5 \title{Handling of foreign C fundamental data types}
|
|
6 \description{Functions to unpack/pack (read/write) foreign C data types from/to R atomic vectors and C data objects such as arrays and pointers to structures.}
|
|
7 \usage{
|
|
8 .pack(x, offset, sigchar, value)
|
|
9 .unpack(x, offset, sigchar)
|
|
10 }
|
|
11 \arguments{
|
|
12 \item{x}{atomic vector (logical, raw, integer or double) or external pointer.}
|
|
13 \item{offset}{integer specifying \emph{byte offset} starting at 0.}
|
|
14 \item{sigchar}{character string specifying the C data type by a \link{type signature}.}
|
|
15 \item{value}{R object value to be coerced and packed to a foreign C data type.}
|
|
16 }
|
|
17 \details{
|
|
18 The function \code{.pack} converts an R \code{value} into a C data type specified by the \link{signature} \code{sigchar}
|
|
19 and it writes the raw C foreign data value at byte position \code{offset} into the object \code{x}.
|
|
20 The function \code{.unpack} extracts a C data type according to the \link{signature} \code{sigchar}
|
|
21 at byte position \code{offset} from the object \code{x} and converts the C value to an R value and returns it.
|
|
22
|
|
23 Byte \code{offset} calculations start at 0 relative to the first byte in an atomic vectors data area.
|
|
24
|
|
25 If \code{x} is an atomic vector, a bound check is carried out before read/write access.
|
|
26 Otherwise, if \code{x} is an external pointer, there is only a C NULL pointer check.
|
|
27 }
|
|
28 \value{
|
|
29 \code{.unpack} returns a read C data type coerced to an R value.
|
|
30 }
|
|
31 \seealso{
|
|
32 \code{\link{.dyncall}} for details on type signatures.
|
|
33 }
|
|
34 \examples{
|
|
35 # transfer double to array of floats and back, compare precision:
|
|
36 n <- 6
|
|
37 input <- rnorm(n)
|
|
38 buf <- raw(n*4)
|
|
39 for (i in 1:n) {
|
|
40 .pack(buf, 4*(i-1), "f", input[i])
|
|
41 }
|
|
42 output <- numeric(n)
|
|
43 for (i in 1:n) {
|
|
44 output[i] <- .unpack(buf, 4*(i-1), "f")
|
|
45 }
|
|
46 # difference between double and float
|
|
47 difference <- output-input
|
|
48 print( cbind(input,output,difference) )
|
|
49 }
|