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
How to read a binary file bit by bit?
3 messages · Inigo Pagola Barrio, Mario Valle, Duncan Murdoch
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:
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 ______________________________________________ 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.
Ing. Mario Valle Data Analysis and Visualization Group | http://www.cscs.ch/~mvalle Swiss National Supercomputing Centre (CSCS) | Tel: +41 (91) 610.82.60 v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax: +41 (91) 610.82.82
Inigo Pagola Barrio wrote:
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?
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