Last active
February 14, 2020 02:08
-
-
Save rugbyprof/7847540 to your computer and use it in GitHub Desktop.
zeros and ones to a file (huffman helper)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
// Write a binary "string" to a file as binary. Used in conjunction with a previous Huffman encoding project. | |
using namespace std; | |
int main(){ | |
ofstream fout; | |
fout.open("output.dat",ios::binary); | |
std::vector<bool> a; | |
string Huffman = "00110000001100010011001000110011"; | |
for(int i=0;i<Huffman.size();i++){ | |
a.push_back(Huffman[i]-48); | |
} | |
for (std::vector<bool>::iterator it = a.begin(); it != a.end();) | |
{ | |
char b = 0; | |
for (int i = 0; i < 8*sizeof(b); ++i) | |
{ | |
b |= (*it & 1) << (8*sizeof(b) - 1 - i); | |
++it; | |
} | |
fout<<b; | |
//flush 'b' | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment