Output bit data to C ++ binary

I am writing a compression program and I need to write bit data to a binary using C ++. If anyone could advise an expression on the record or a website with tips, I would be very grateful.

Sorry if this is a simple or confusing question, I am struggling to find answers on the Internet.

+6
c ++ bit-manipulation huffman-code
source share
3 answers

Collect bits into integer bytes, such as unsigned char or std :: bitset (where the bit size is a multiple of CHAR_BIT), and then write the integer bytes at a time. Computers "deal with bits," but the abstraction available - especially for IO - is that you, as a programmer, deal with individual bytes. Bitwise manipulation can be used to switch certain bits, but you always process objects in size in bytes.

At the end of the output, if you don't have an integer byte, you need to decide how it should be saved. Both iostreams and stdio can write raw data using ostream :: write and fwrite respectively.

Instead of a single char or bit set <8> (8 is the most common value for CHAR_BIT), you might consider using a larger block size, such as an array of 4-32 or more, characters, or an equivalent bit size.

+3
source share

For writing binary code, the trick I found most useful is to save all the binary files as a single array in memory, and then move it to your hard drive. Executing bits at a time or bytes at a time or unsigned long at a time is not as fast as having all the data stored in the array and using one instance of "fwrite ()" to store it on your hard drive.

size_t fwrite (const void * ptr, size_t size, size_t count, stream FILE *);

Link: http://www.cplusplus.com/reference/clibrary/cstdio/fwrite/

In English:

fwrite ([array * of stored data], [size in bytes of the OBJECT array. for unsigned chars → 1, for unsigned long longs → 8], [number of instances in the array], [FILE *])

Always check your results to confirm success!

In addition, an argument can be made that the maximum type of the object should be as large as possible ([unsigned long long]> [char]). Although I do not understand the coding behind "fwrite ()", I believe that the time to convert from the natural object used in your code to [unsigned long long] will take more time in combination with writing than "fwrite ()" for account of what you have.

When I studied Huffman coding, it took me a few hours to realize that there was a difference between [char] and [unsigned char]. Note this method that you should always use unsigned variables to store a clean binary.

+2
source share

below class you can write and read in half

class bitChar{ public: unsigned char* c; int shift_count; string BITS; bitChar() { shift_count = 0; c = (unsigned char*)calloc(1, sizeof(char)); } string readByBits(ifstream& inf) { string s =""; char buffer[1]; while (inf.read (buffer, 1)) { s += getBits(*buffer); } return s; } void setBITS(string X) { BITS = X; } int insertBits(ofstream& outf) { int total = 0; while(BITS.length()) { if(BITS[0] == '1') *c |= 1; *c <<= 1; ++shift_count; ++total; BITS.erase(0, 1); if(shift_count == 7 ) { if(BITS.size()>0) { if(BITS[0] == '1') *c |= 1; ++total; BITS.erase(0, 1); } writeBits(outf); shift_count = 0; free(c); c = (unsigned char*)calloc(1, sizeof(char)); } } if(shift_count > 0) { *c <<= (7 - shift_count); writeBits(outf); free(c); c = (unsigned char*)calloc(1, sizeof(char)); } outf.close(); return total; } string getBits(unsigned char X) { stringstream itoa; for(unsigned s = 7; s > 0 ; s--) { itoa << ((X >> s) & 1); } itoa << (X&1) ; return itoa.str(); } void writeBits(ofstream& outf) { outf << *c; } ~bitChar() { if(c) free(c); } }; 

for example

 #include <iostream> #include <sstream> #include <fstream> #include <string> #include <stdlib.h> using namespace std; int main() { ofstream outf("Sample.dat"); ifstream inf("Sample.dat"); string enCoded = "101000001010101010"; //write to file cout << enCoded << endl ; //print 101000001010101010 bitChar bchar; bchar.setBITS(enCoded); bchar.insertBits(outf); //read from file string decoded =bchar.readByBits(inf); cout << decoded << endl ; //print 101000001010101010000000 return 0; } 
+1
source share

All Articles