Write 9 bits binary data in C -


i trying write file binary data not fit in 8 bits. understand can write binary data of length if can group in predefined length of 8, 16, 32,64. there way write 9 bits file? or 2 values of 9 bits?

i have 1 value in range -+32768 , 3 values in range +-256. way save space?

thank you

no, don't think there's way using c's file i/o api:s express storing less 1 char of data, typically 8 bits.

if you're on 9-bit system, char_bit 9, trivial.

if you're asking "how can store number has limited range using precise number of bits needed", inside possibly larger file, that's of course possible.

this called bitstreaming , way optimize space used information. encoding/decoding bitstream formats requires keep track of how many bits have "consumed" of current input/output byte in actual file. it's bit complicated not hard.

basically, you'll need:

  • a byte stream s, i.e. can put bytes into, such file *.
  • a bit index i, i.e. unsigned value keeps track of how many bits you've emitted.
  • a current byte x, bits can put, each time incrementing i. when i reaches char_bit, write s , reset i zero.

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -