4nti7rust
4nti7rust

Reputation: 116

LUA - Read a binary file bit by bit

You can read a binary file byte by byte using f:read(size) in which size represent the number of byte but how can I read it bit by bit ? (1/8 of byte or octet if you want)

It's ok for most of the data Int8(1),Uint16(2),Uint32(4),Int*(4) But for BOOL(0.125 ?).

Thanks for your help !

EDIT : My problem is obviously not to read the file bit by bit but to manage to extract all the data, including some boolean value (without creating a "shift" of 1 bit).

Upvotes: 1

Views: 2591

Answers (1)

bames53
bames53

Reputation: 88155

You can't. A byte is the smallest unit that can be read from a file. You could write code that wraps the bytewise access to make it look bitwise.

The appropriate way to read a boolean value from a file depends on how it was written. Unless you go to the exact same sort of trouble to write boolean values bit by bit then there's no need to read them that way.

The Lua file API only deals in numbers and strings. To write a boolean value you'd convert it to one of these types. To read it you would read one of these types and the perform the inverse of the conversion you used for writing. For example you might convert true to 1 and false to 0, and then write and read numbers. Or you might try to pack several boolean values into one number. In any case, you don't need to read or write the file bit by bit to read and write boolean data.

Upvotes: 1

Related Questions