Skip to content

How to read a binary file bit by bit?

3 messages · Inigo Pagola Barrio, Mario Valle, Duncan Murdoch

#
Hello everybody,

I am trying to a read a binary file with different formats. I use the readBin function so I can read bytes, short and double numbers depending on the bytes per element in the byte stream. But now I need to read bit by bit, and join them in groups of ten because every ten bits will form a number. How can I do this?

Regards,

_____________________________________________________________________
I?igo Pagola
Solar Thermal Energy Deparment
CENTRO NACIONAL DE ENERG?AS RENOVABLES
#
In my humble opinion, forget R and use more appropriate tools, like a 
very small C program that reads 10 bytes at a time and outputs 8 integers.
Then use R to read the resulting file.
Ciao!
          mario
Inigo Pagola Barrio wrote:

  
    
#
Inigo Pagola Barrio wrote:
There are packages that perform bit operations, but I don't think you 
need that.  Since 5 bytes make 4 of your numbers, why not read the data 
as bytes, then convert in groups of 5?  E.g. if b is a vector of bytes, 
then the first of your numbers is something like b[1] + 256*(b[2] %% 
4).  The second would be b[2] %/% 4 + 64*(b[3] %% 16).  (These might not 
be the right formulas:  there are lots of ways to pack10 bit numbers 
into bytes.  You need to know which one was used in your file.)

Duncan Murdoch