[R] readBin which has two different sizes

Henrik Bengtsson hb at stat.berkeley.edu
Sun Oct 3 20:51:20 CEST 2010


On Sun, Oct 3, 2010 at 10:59 AM, Ab Hu <master.rstat at yahoo.com> wrote:
>
> Hi,
> I have a binary file which has the following structure:
> 1) Some header in the beginning
> 2) Thousands of 216-byte data sub-grouped into 4 54-byte data structured as
> 4-byte time stamp (big endian) followed by 50 1-byte (8-bit) samples.
>
> So far this is how I am trying:
> #Open a connection for binary file
> to.read <- file("binary file", "rb")
>
> #Read header
> info <- readBin(to.read, character(),1)
>
> #Read data: byte=1, n=number of data (i know this from header) * 216 (bytes
> per data)
> data <- readBin(to.read, integer(), size=1, n=35269*216, signed=TRUE, endian
> = "big")
>
> I am able to read the header but obviously having trouble in that last line
> because my data has two sizes; 4-byte time stamp and one byte (8-bit)
> samples. Also, one is unsigned and other is signed.

Outline:

1. Use x <- readBin(..., what="raw", n=35269*(54*4)) to read your raw
("byte") data.
2. Turn it into a 54x4x35269 array, e.g. dim(x) <- c(54,4,35269).
3. Extract the 4-byte time stamps by yT <- x[1:4,,,drop=FALSE];  This
is of type "raw".  Use readBin() to parse it, i.e. zT <- readBin(yT,
what="integer", size=4, signed=TRUE, endian="big").  There are your
timestamps.
4. Extract the 50 1-byte samples by yS <- x[5:54,,,drop=FALSE];  zS <-
readBin(yS, what="integer", size=1, signed=FALSE);

Something like that.

/Henrik

>
> How do i read these two differently sized, signed data?
> Would appreciate any help, thanks.
> --
> View this message in context: http://r.789695.n4.nabble.com/readBin-which-has-two-different-sizes-tp2953365p2953365.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list