Advertisement
Heretiiik

splitter.cpp

Jun 5th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <string>
  3. #include <iostream>
  4. #include <vector>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. struct Bit {
  10.     unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
  11. };
  12. union CharBit {    
  13.     char byte;
  14.     Bit bits;
  15. };
  16.  
  17. vector<string> split ( string str ) {
  18.     vector<string> tmp;
  19.     int last = 0;
  20.     for (int i = 0; i < str.size(); ++i) {
  21.         if ( str[i] == '#' ) {
  22.             tmp.push_back( str.substr( last, i - last ) );
  23.             last = i+1;
  24.         }
  25.         if (tmp.size() < 1)
  26.             continue;
  27.         if (tmp.back().size() != 8)
  28.             tmp.pop_back();
  29.     }
  30.     return tmp;
  31. }
  32.  
  33. string decodestr ( string str ) {
  34.     string s = "";
  35.     //--------------------------------
  36.     vector<string> pole = split ( str );
  37.     //--------------------------------    
  38.     for (int i = 0; i < pole.size(); ++i) {
  39.         //----------------------------
  40.         char c = 0;
  41.         unsigned char sub = 128;
  42.         for (int j = 0; j < 8; ++j) {
  43.             if (pole[i][j] == '1') {
  44.                 c += sub;
  45.             }
  46.             sub = sub / 2;
  47.             //sub = sub >> 1;
  48.         }
  49.         s+=c;
  50.         //-----------------------------
  51.         /*char c = 0;
  52.         for (int j = 0; j < 8; ++j) {
  53.             if (pole[i][j] == '1') {
  54.                 c += pow(2,7-j);
  55.             }
  56.         }
  57.         s+=c;*/
  58.         //-----------------------------
  59.         /*CharBit byte;
  60.         byte.bits.b0 = pole[i][7] - 48;
  61.         byte.bits.b1 = pole[i][6] - 48;
  62.         byte.bits.b2 = pole[i][5] - 48;
  63.         byte.bits.b3 = pole[i][4] - 48;
  64.         byte.bits.b4 = pole[i][3] - 48;
  65.         byte.bits.b5 = pole[i][2] - 48;
  66.         byte.bits.b6 = pole[i][1] - 48;
  67.         byte.bits.b7 = pole[i][0] - 48;
  68.         s+=byte.byte;*/
  69.         //-----------------------------
  70.     }
  71.     return s;
  72. }
  73.  
  74. string decodestr2 (string str) {
  75.     string s = "";
  76.     string tmp;
  77.     int last;
  78.     for (int i = 0; i < str.size(); ++i) {
  79.         if ( str[i] == '#' ) {
  80.             tmp = str.substr( last, i - last );
  81.             last = i+1;
  82.            
  83.             if (tmp.size() == 8) {
  84.                 //----------------------------
  85.                char c = 0;
  86.                unsigned char sub = 128;
  87.                for (int j = 0; j < 8; ++j) {
  88.                    if (tmp[j] == '1') {
  89.                        c += sub;
  90.                    }
  91.                    sub = sub / 2;
  92.                    //sub = sub >> 1;
  93.                }
  94.                s+=c;
  95.                //-----------------------------
  96.                /*char c = 0;
  97.                for (int j = 0; j < 8; ++j) {
  98.                    if (tmp[j] == '1') {
  99.                        c += pow(2,7-j);
  100.                    }
  101.                }
  102.                s+=c;*/
  103.                //-----------------------------                  
  104.             }
  105.         }    
  106.     }
  107.     return s;
  108. }
  109.  
  110.  
  111. int main(int argc, char** argv) {
  112.     string vstup = "#01010101#011110#01010101#";
  113.     cout << decodestr2 (vstup) << endl;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement