Skip to content

Help with readBin

6 messages · Duncan Murdoch, kapo coulibaly, William Dunlap

#
On 03/05/2012 12:41 PM, kapo coulibaly wrote:
As I wrote to someone else with a similar problem a couple of weeks ago:

You need to see what's in the file.  The hexView package can dump it in
various formats; see example(viewFormat) for a couple.

Duncan Murdoch
#
On 03/05/2012 1:57 PM, kapo coulibaly wrote:
The first two doubles read properly as 1, and the next one as 33.674, 
when I follow your description above on the dump you sent me 
privately.   I'm on a system with endian="little"; you might want to 
specify that explicitly in your readBin calls.

And please, in future, spend some time making your questions easier to 
answer:  put the dump in the public message.  Don't post irrelevant 
code, extract just the bit that's doing the reading.

Duncan Murdoch
#
You can do the following to allow others to recreate your problem.

  yourFileBytes <- readBin("yourFile", what="integer", size=1, n=300) # is 300 bytes enough to see the problem?
  dput(yourFileBytes)

Put the output of dput(yourFileBytes) in your mail.  Someone can (and you should)
recreate the problem with
  bytes <- ... copy 'n paste the printout of dput(bytes) here ...
  tf <- tempfile()
  stopifnot(is.integer(bytes) && all(abs(bytes)<=128)) # to make sure bytes was copied correctly
  writeBin(bytes, con=tf, size=1)

Then show just the commands needed to read a couple of rows of your file, along with
the expected output, as precisely and you can.  E.g.,
  con <- file(tf, "rb")
  readBin(con, what="integer", size=4, n=2) # expect 3 then something less than 10
  readBin(con, what="numeric", size=8, n=3) # expect 2 numbers in range (0, 32] then 2.57
  ...

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com