Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <bitset>
- using namespace std;
- static inline void reduceFirstZero(string& text)
- {
- int counter = 0;
- while (text[counter] == '0')
- {
- counter++;
- }
- if (counter)
- {
- text = text.substr(counter, text.size());
- }
- }
- static inline const string hexCharToBin(char c)
- {
- switch (c)
- {
- case '0': return "0000";
- case '1': return "0001";
- case '2': return "0010";
- case '3': return "0011";
- case '4': return "0100";
- case '5': return "0101";
- case '6': return "0110";
- case '7': return "0111";
- case '8': return "1000";
- case '9': return "1001";
- case 'A': return "1010";
- case 'B': return "1011";
- case 'C': return "1100";
- case 'D': return "1101";
- case 'E': return "1110";
- case 'F': return "1111";
- }
- }
- static inline string hexToBin(string hex)
- {
- string binary;
- int hexl = hex.length();
- for (int i = 0; i < hexl; i++)
- {
- binary += hexCharToBin(hex[i]);
- }
- reduceFirstZero(binary);
- return binary;
- }
- static inline string binToHex(string block)
- {
- stringstream ss;
- int blockl = block.length();
- for (int i = 0; i < blockl; i += 4)
- {
- bitset<4> set(block.substr(i,i+4));
- ss << hex << uppercase << set.to_ulong();
- }
- return ss.str();
- }
- static inline string textToBin(string text)
- {
- string binary;
- int textl = text.length();
- for (int i = 0; i < textl; i++)
- {
- binary+=(bitset<8>(text.c_str()[i]).to_string());
- }
- reduceFirstZero(binary);
- return binary;
- }
- static inline string XOR(string first, string second, int size)
- {
- string solution;
- for (int i = 0; i < size; i++)
- {
- (first[i] == second[i]) ? solution+='0' : solution+='1';
- }
- return solution;
- }
- static inline void crc(string& wx, string px)
- {
- string tempRes;
- reduceFirstZero(wx);
- while (wx.length() >= px.length())
- {
- tempRes = XOR(wx, px, px.length());
- for (int i = 0; i < tempRes.length(); i++)
- {
- wx[i] = tempRes[i];
- }
- reduceFirstZero(wx);
- }
- }
- int main()
- {
- int t,pxl;
- string text;
- string px;
- string wx;
- cin >> t;
- for (int i = 0; i < t; i++)
- {
- cin >> px >> text;
- px = hexToBin(px);
- wx = textToBin(text);
- pxl = px.length() - 1;
- for (int i = 0; i < pxl; i++)
- {
- wx += '0';
- }
- crc(wx, px);
- cout << binToHex(wx) << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement