Advertisement
noorary

Untitled

Mar 29th, 2020
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <bitset>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. vector<ulong> decoded;
  9. int counter = 0;
  10. int counter2 = 900;
  11.  
  12. // ENCODING METHOD
  13.  
  14. vector<uint8_t> encoding(long i) {
  15.  
  16. vector<uint8_t> vector;
  17. int foofoo = 0;
  18.  
  19. while(true) {
  20.  
  21. ulong b = i % 128;
  22.  
  23.  
  24. if(i < 128) {
  25. b = b +128;
  26. vector.insert(vector.begin(), b);
  27.  
  28.  
  29. break;
  30. }
  31.  
  32. vector.insert(vector.begin(), b);
  33. i = i / 128;
  34.  
  35.  
  36. }
  37. return vector;
  38.  
  39. }
  40.  
  41. // DECODING METHOD
  42.  
  43. int decoding(vector<uint8_t> vector2) {
  44.  
  45. ulong result;
  46.  
  47. ulong apu = vector2.at(vector2.size()-1);
  48. //cout << apu << endl;
  49.  
  50. result = apu - 128;
  51. result *= 128;
  52.  
  53. int i=vector2.size()-2;
  54.  
  55. /*if(counter < 1) {
  56. cout << i << endl;
  57. counter++;
  58. } */
  59.  
  60.  
  61. for(i; i >-1; i++) {
  62. result += vector2.at(i);
  63. result *= 128;
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70. decoded.push_back(result);
  71.  
  72.  
  73.  
  74.  
  75. }
  76.  
  77. // WRITING METHOD (F0.vb.dec)
  78.  
  79. int writedec(vector<ulong> helpvec) {
  80.  
  81. ofstream writefiledec ("F0.vb.dec", ios::out | ios::binary);
  82.  
  83. if(!writefiledec) {
  84. cout << "Cannot open file!" << endl;
  85. return 1;
  86. }
  87.  
  88. while(!helpvec.empty()) {
  89. ulong b = helpvec.front();
  90. // cout << b << endl;
  91. helpvec.erase(helpvec.begin());
  92. writefiledec.write((char *) &b, sizeof(b));
  93. }
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100. // WRITING METHOD (F0.vb)
  101.  
  102. int writevb(vector<uint8_t> vbyte) {
  103.  
  104. ofstream writefile ("F0.vb", ios::out | ios::binary | ios::app);
  105.  
  106. if(!writefile) {
  107. cout << "Cannot open file!" << endl;
  108. return 1;
  109. }
  110.  
  111. writefile.write((char *) &vbyte[0], vbyte.size() * sizeof(uint8_t));
  112.  
  113. }
  114.  
  115. // MAIN METHOD
  116.  
  117. int main() {
  118.  
  119.  
  120. // ENCODING
  121.  
  122. ulong x;
  123.  
  124. ifstream file ("F0", ios::out | ios::binary);
  125.  
  126. if(!file) {
  127. cout << "Cannot open file" << endl;
  128. return 1;
  129. }
  130.  
  131. while(file.read( (char *) &x, sizeof(x))) {
  132.  
  133. vector<uint8_t> vbytes = encoding(x);
  134. writevb(vbytes);
  135. }
  136.  
  137. file.close();
  138.  
  139. // DECODING
  140.  
  141. ulong y;
  142.  
  143. ifstream file2("F0.vb", ios::out | ios::binary);
  144.  
  145. if(!file2) {
  146. cout << "Cannot open file2" << endl;
  147. return 1;
  148. }
  149.  
  150. while(file2.read( (char *) &y, sizeof(y))) {
  151.  
  152.  
  153. vector<uint8_t> help;
  154.  
  155. if(y & (1 << 7)) {
  156. // cout << "Kahdeksas bitti on 1" << endl;
  157. help.push_back(y);
  158.  
  159. decoding(help);
  160. help.clear();
  161. } else {
  162. help.push_back(y);
  163. }
  164. }
  165.  
  166. writedec(decoded);
  167.  
  168. file2.close();
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement