Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>by below class you can write and read bit by bit </p> <pre><code>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&amp; 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&amp; outf) { int total = 0; while(BITS.length()) { if(BITS[0] == '1') *c |= 1; *c &lt;&lt;= 1; ++shift_count; ++total; BITS.erase(0, 1); if(shift_count == 7 ) { if(BITS.size()&gt;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 &gt; 0) { *c &lt;&lt;= (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 &gt; 0 ; s--) { itoa &lt;&lt; ((X &gt;&gt; s) &amp; 1); } itoa &lt;&lt; (X&amp;1) ; return itoa.str(); } void writeBits(ofstream&amp; outf) { outf &lt;&lt; *c; } ~bitChar() { if(c) free(c); } }; </code></pre> <p>for <code>example</code> </p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;stdlib.h&gt; using namespace std; int main() { ofstream outf("Sample.dat"); ifstream inf("Sample.dat"); string enCoded = "101000001010101010"; //write to file cout &lt;&lt; enCoded &lt;&lt; endl ; //print 101000001010101010 bitChar bchar; bchar.setBITS(enCoded); bchar.insertBits(outf); //read from file string decoded =bchar.readByBits(inf); cout &lt;&lt; decoded &lt;&lt; endl ; //print 101000001010101010000000 return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload