[R] How I can read the binary file with "different type"?

Henrik Bengtsson hb at stat.berkeley.edu
Fri Aug 22 07:07:55 CEST 2008


Wow, one of those might binary file format likely to be dumped from
memory by a C/C++ program.  ...anyway, here is how I deal with
interveawed data types:

# Read all of the file as raw (bytes) values
fileSize <- file.info(pathname)$size;
raw <- readBin(pathname, what="raw", n=fileSize);
# Sanity check
if (length(raw) %% 8 != 0)
  stop("File format assumption error: ", pathname);
# Turn into an 8xN matrix; each column is a (integer, float) record
nbrOfRecords <- length(raw) %/% 8;
dim(raw) <- c(8, nbrOfRecords);
# First four rows are the integers
ints <- readBin(con=raw[1:4,], what="integer", size=4, n=nbrOfRecords,
endian="little");
# Last four rows are the floats
floats <- readBin(con=raw[5:8,], what="double", size=4,
n=nbrOfRecords, endian="little");
rm(raw);

The nice thing here is that readBin() can take filenames, connections
and raw vectors - we use the latter.

/Henrik


On Thu, Aug 21, 2008 at 8:36 PM, Moshe Olshansky <m_olshansky at yahoo.com> wrote:
> Hi Miao,
>
> I can write a function which takes an integer and produces a float number whose binary representation equals to that of the integer, but this would be an awkward solution.
> So if nobody suggests anything better I will write such a function for you, but let's wait for a better solution.
>
>
> --- On Fri, 22/8/08, Bingxiang Miao <miao2007 at gmail.com> wrote:
>
>> From: Bingxiang Miao <miao2007 at gmail.com>
>> Subject: [R] How I can read the binary file with "different type"?
>> To: r-help at r-project.org
>> Received: Friday, 22 August, 2008, 11:51 AM
>> Hi all,
>>
>> I  have a binary file  which have 8*100 bytes. The
>> structure of the file is
>> as follows: every eigth bytes represent 2 data:the first
>> four bytes is the
>> little-endian for integer, the next four bytes is the
>> little-endian for
>> floating32. The structure of the following bytes is as the
>> same as the first
>> eight bytes'.
>>
>> As the function readBin only read the binary file with one
>> structure like
>> this: readBin("my data
>> file",what="integer",size=4,n=200) or
>> readBin("my
>> data file",what="numeric",size=4,n=200).But
>> only one type of the data can be
>> read properly.
>>
>> Can anyone suggest me how should I read this file?
>>
>> Thanks in advance.
>>
>> Miao
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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