Skip to content

Instantly share code, notes, and snippets.

@rugbyprof
Last active February 14, 2020 02:08
Show Gist options
  • Save rugbyprof/7847540 to your computer and use it in GitHub Desktop.
Save rugbyprof/7847540 to your computer and use it in GitHub Desktop.
zeros and ones to a file (huffman helper)
#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