[R] How to write the bytes 00 01 00 to a file/connection?
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Tue Jul 17 09:06:28 CEST 2001
On Mon, 16 Jul 2001, Henrik Bengtsson wrote:
> Is there any way to write (8-bit) bytes to a file which works on all [R]
> platforms? I have been looking at
>
> 1) writeBin
> 2) writeChar
> 3) cat
>
> and neither of them manage to write arbitrary sequences of bytes (0-255).
>
You could also use
library(Rstreams)
s <- openstream("foo", "write")
writeint(s, bfr, 1)
closestream(s)
> For instance, I would like to create a binary file of length three
> containing the bytes 0, 1 and 0. I [R] I have the following vector of bytes:
>
> bfr <- c(0,1,0)
Nope, that's a sequence of doubles and that seems to be the problem.
> 1) For having 'writeBin' to write bytes to a file the argument 'size' should
> be one. However, this is not supported by my Windows Me system. I haven't
> been able to try this on other platforms. Using 'writeBin(bfr, con)' with
> default 'size' argument will generate a file containing 00 00 00 01 00 00,
> i.e. two bytes for every integer. One approach would be to convert 8-bit
> bytes into 16-bit integers and then write the integers. However, this would
> only work for file sizes of even length!
You need them to be *integers*
bfr <- as.integer(c(0,1,0))
out <- file("foo", "wb")
writeBin(bfr, out, size=1)
close(out)
works for me (including on Win2K: I have never had to use Windows Me).
That's the intended approach.
[...]
--
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 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list