Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a binary file bit by bit

Tags:

c

file

binary

bin

I know the function below:

size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);

It only reads byte by byte, my goal is to be able to read 12 bits at a time and then take them into an array. Any help or pointers would be greatly appreciated!

like image 262
Ryan Avatar asked Jul 27 '12 01:07

Ryan


People also ask

How do you read a binary file as a bit?

Read the first two bytes from your a_file file pointer and check the bits in the least or greatest byte — depending on the endianness of your platform (x86 is little-endian) — using bitshift operators. You can't really put bits into an array, as there isn't a datatype for bits.

How do I read a bit file in C++?

To read a binary file in C++ use read method. It extracts a given number of bytes from the given stream and place them into the memory, pointed to by the first parameter. If any error is occurred during reading in the file, the stream is placed in an error state, all future read operation will be failed then.

How do you read a single bit?

To read a bit at a specific position, you must mask out all other bits in the value. The operator that assists in that process is the bitwise & (and). After you mask out all the other bits, the value that remains is either zero or some other value.

Can you read binary file without knowing the data format?

You need to know the format of the binary file and how many bytes to read. There is no way for the stream itself to know how many bytes to read based on the file contents. So you basically have to know the order of your data and read the file like you are doing.

How do you read a binary file in Python?

Python read a binary file. Here, we will see how to read a binary file in Python. Before reading a file we have to write the file. In this example, I have opened a file using file = open(“document.bin”,”wb”) and used the “wb” mode to write the binary file. The document.bin is the name of the file.

How to read binary file in RB mode?

1 To read the file, I have taken the already created file document.bin and used the “rb” mode to read the binary file. 2 The document.bin is the file name. And, I have using the read () method. The read () method returns the specified number... More ...

How do you read binary numbers?

The best way to read a binary number is to start with the right-most digit, and work your way left. The power of that first location is zero, meaning the value for that digit, if it's not a zero, is two to the power of zero, or one. In this case, since the digit is a zero, the value for this place would be zero.

How many bytes does it take to read a 12 bit pointer?

As a complication, on subsequent reads, if you want to fread through the pointer every 12 bits, you would read only one byte, using the left-over bits from the previous read to build a new 12-bit value. If there are no leftovers, you would need to read two bytes.


1 Answers

Adding to the first comment, you can try reading one byte at a time (declare a char variable and write there), and then use the bitwise operators >> and << to read bit by bit. Read more here: http://www.cprogramming.com/tutorial/bitwise_operators.html

like image 138
Carlos Vergara Avatar answered Oct 07 '22 13:10

Carlos Vergara