Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <bitset>
  6.  
  7. using namespace std;
  8.  
  9. static inline void reduceFirstZero(string& text)
  10. {
  11.     int counter = 0;
  12.    
  13.     while (text[counter] == '0')
  14.     {
  15.         counter++;
  16.     }
  17.  
  18.     if (counter)
  19.     {
  20.         text = text.substr(counter, text.size());
  21.     }
  22. }
  23.  
  24. static inline const string hexCharToBin(char c)
  25. {
  26.     switch (c)
  27.     {
  28.     case '0': return "0000";
  29.     case '1': return "0001";
  30.     case '2': return "0010";
  31.     case '3': return "0011";
  32.     case '4': return "0100";
  33.     case '5': return "0101";
  34.     case '6': return "0110";
  35.     case '7': return "0111";
  36.     case '8': return "1000";
  37.     case '9': return "1001";
  38.     case 'A': return "1010";
  39.     case 'B': return "1011";
  40.     case 'C': return "1100";
  41.     case 'D': return "1101";
  42.     case 'E': return "1110";
  43.     case 'F': return "1111";
  44.     }
  45. }
  46.  
  47. static inline string hexToBin(string hex)
  48. {
  49.     string binary;
  50.     int hexl = hex.length();
  51.     for (int i = 0; i < hexl; i++)
  52.     {
  53.         binary += hexCharToBin(hex[i]);
  54.     }
  55.  
  56.     reduceFirstZero(binary);
  57.  
  58.     return binary;
  59. }
  60.  
  61. static inline string binToHex(string block)
  62. {
  63.     stringstream ss;
  64.     int blockl = block.length();
  65.     for (int i = 0; i < blockl; i += 4)
  66.     {
  67.         bitset<4> set(block.substr(i,i+4));
  68.         ss << hex << uppercase << set.to_ulong();
  69.     }
  70.  
  71.     return ss.str();
  72. }
  73.  
  74. static inline string textToBin(string text)
  75. {
  76.     string binary;
  77.     int textl = text.length();
  78.     for (int i = 0; i < textl; i++)
  79.     {
  80.         binary+=(bitset<8>(text.c_str()[i]).to_string());
  81.     }
  82.  
  83.     reduceFirstZero(binary);
  84.  
  85.     return binary;
  86. }
  87.  
  88. static inline string XOR(string first, string second, int size)
  89. {
  90.     string solution;
  91.  
  92.     for (int i = 0; i < size; i++)
  93.     {
  94.         (first[i] == second[i]) ? solution+='0' : solution+='1';
  95.     }
  96.  
  97.     return solution;
  98.  
  99. }
  100.  
  101. static inline void crc(string& wx, string px)
  102. {
  103.     string tempRes;
  104.  
  105.     reduceFirstZero(wx);
  106.  
  107.     while (wx.length() >= px.length())
  108.     {
  109.         tempRes = XOR(wx, px, px.length());
  110.  
  111.         for (int i = 0; i < tempRes.length(); i++)
  112.         {
  113.             wx[i] = tempRes[i];
  114.         }
  115.  
  116.         reduceFirstZero(wx);
  117.     }
  118.  
  119. }
  120.  
  121. int main()
  122. {
  123.     int t,pxl;
  124.  
  125.     string text;
  126.     string px;
  127.     string wx;
  128.  
  129.     cin >> t;
  130.  
  131.     for (int i = 0; i < t; i++)
  132.     {
  133.         cin >> px >> text;
  134.         px = hexToBin(px);
  135.         wx = textToBin(text);
  136.         pxl = px.length() - 1;
  137.         for (int i = 0; i < pxl; i++)
  138.         {
  139.             wx += '0';
  140.         }
  141.         crc(wx, px);
  142.         cout << binToHex(wx) << endl;
  143.     }
  144.  
  145.     system("pause");
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement