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. 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.
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. >
Henrik Bengtsson wrote:> > > 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 >Tried it and it works great. I had to make just one adjustment, i.e. specify the size of n, which I did using length: yT <- x[1:4,,,drop=FALSE] zT <- readBin(yT, what="integer", size=4, n=length(yT), signed=FALSE, endian="big") yS <- x[5:54,,,drop=FALSE]; zS <- readBin(yS, what="integer", size=1, n=length(yS), signed=TRUE) Thanks Henrik! -- View this message in context: http://r.789695.n4.nabble.com/readBin-which-has-two-different-sizes-tp2953365p2953721.html Sent from the R help mailing list archive at Nabble.com.