[R] Reading binary files

Rui Barradas ruipbarradas at sapo.pt
Tue Jun 12 14:09:31 CEST 2012


Hello,

 From the help page for readBin:

"readBin and writeBin read and write C-style zero-terminated character 
strings."


Your description of the string doesn't match this one. To read any sort 
of character strings, use readChar.

readChar(to.read, nchars = 4, useBytes = TRUE)

Many times, when I have problems with readBin, this works.

Hope this helps,

Rui Barradas

Em 12-06-2012 08:51, Nortisiv escreveu:
> Hey guys,
>
> I am currently trying to read in a binary file using this helpful manual:
> http://www.ats.ucla.edu/stat/r/faq/read_binary.htm Reading binary files
>
> I also have the detailed format description from the developer. But somehow
> the reading of the file does still not work properly. The first entry in the
> file is the following, according to the manufacturer:
> "String containing 4 one-byte characters."
> The content should be "ABF", as I was able to confirm with an external
> Hex-Editor, so in general everything is correct.
> But when I try to read out the first entry in R, I can see that R reads the
> first 12 bytes, although I tell it to read only 4 bytes, using the "Size"
> option (as I found out using the Hex-Editor):
>
>> to.read = file("test.DAT", "rb")
>> readBin(to.read, character(), size=4, endian = "little")
> [1] "ABF ff¦?\005"
>
> Why does R do this? Changing the size parameter seems to have no efect on
> the output "ABF ff¦?\005". Any idea whats going wrong?
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Reading-binary-files-tp4633078.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