Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <bitset>
- #include <vector>
- using namespace std;
- vector<ulong> decoded;
- int counter = 0;
- int counter2 = 900;
- // ENCODING METHOD
- vector<uint8_t> encoding(long i) {
- vector<uint8_t> vector;
- int foofoo = 0;
- while(true) {
- ulong b = i % 128;
- if(i < 128) {
- b = b +128;
- vector.insert(vector.begin(), b);
- break;
- }
- vector.insert(vector.begin(), b);
- i = i / 128;
- }
- return vector;
- }
- // DECODING METHOD
- int decoding(vector<uint8_t> vector2) {
- ulong result;
- ulong apu = vector2.at(vector2.size()-1);
- //cout << apu << endl;
- result = apu - 128;
- result *= 128;
- int i=vector2.size()-2;
- /*if(counter < 1) {
- cout << i << endl;
- counter++;
- } */
- for(i; i >-1; i++) {
- result += vector2.at(i);
- result *= 128;
- }
- decoded.push_back(result);
- }
- // WRITING METHOD (F0.vb.dec)
- int writedec(vector<ulong> helpvec) {
- ofstream writefiledec ("F0.vb.dec", ios::out | ios::binary);
- if(!writefiledec) {
- cout << "Cannot open file!" << endl;
- return 1;
- }
- while(!helpvec.empty()) {
- ulong b = helpvec.front();
- // cout << b << endl;
- helpvec.erase(helpvec.begin());
- writefiledec.write((char *) &b, sizeof(b));
- }
- }
- // WRITING METHOD (F0.vb)
- int writevb(vector<uint8_t> vbyte) {
- ofstream writefile ("F0.vb", ios::out | ios::binary | ios::app);
- if(!writefile) {
- cout << "Cannot open file!" << endl;
- return 1;
- }
- writefile.write((char *) &vbyte[0], vbyte.size() * sizeof(uint8_t));
- }
- // MAIN METHOD
- int main() {
- // ENCODING
- ulong x;
- ifstream file ("F0", ios::out | ios::binary);
- if(!file) {
- cout << "Cannot open file" << endl;
- return 1;
- }
- while(file.read( (char *) &x, sizeof(x))) {
- vector<uint8_t> vbytes = encoding(x);
- writevb(vbytes);
- }
- file.close();
- // DECODING
- ulong y;
- ifstream file2("F0.vb", ios::out | ios::binary);
- if(!file2) {
- cout << "Cannot open file2" << endl;
- return 1;
- }
- while(file2.read( (char *) &y, sizeof(y))) {
- vector<uint8_t> help;
- if(y & (1 << 7)) {
- // cout << "Kahdeksas bitti on 1" << endl;
- help.push_back(y);
- decoding(help);
- help.clear();
- } else {
- help.push_back(y);
- }
- }
- writedec(decoded);
- file2.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement