Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <string>
- #include <iostream>
- #include <vector>
- #include <cmath>
- using namespace std;
- struct Bit {
- unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
- };
- union CharBit {
- char byte;
- Bit bits;
- };
- vector<string> split ( string str ) {
- vector<string> tmp;
- int last = 0;
- for (int i = 0; i < str.size(); ++i) {
- if ( str[i] == '#' ) {
- tmp.push_back( str.substr( last, i - last ) );
- last = i+1;
- }
- if (tmp.size() < 1)
- continue;
- if (tmp.back().size() != 8)
- tmp.pop_back();
- }
- return tmp;
- }
- string decodestr ( string str ) {
- string s = "";
- //--------------------------------
- vector<string> pole = split ( str );
- //--------------------------------
- for (int i = 0; i < pole.size(); ++i) {
- //----------------------------
- char c = 0;
- unsigned char sub = 128;
- for (int j = 0; j < 8; ++j) {
- if (pole[i][j] == '1') {
- c += sub;
- }
- sub = sub / 2;
- //sub = sub >> 1;
- }
- s+=c;
- //-----------------------------
- /*char c = 0;
- for (int j = 0; j < 8; ++j) {
- if (pole[i][j] == '1') {
- c += pow(2,7-j);
- }
- }
- s+=c;*/
- //-----------------------------
- /*CharBit byte;
- byte.bits.b0 = pole[i][7] - 48;
- byte.bits.b1 = pole[i][6] - 48;
- byte.bits.b2 = pole[i][5] - 48;
- byte.bits.b3 = pole[i][4] - 48;
- byte.bits.b4 = pole[i][3] - 48;
- byte.bits.b5 = pole[i][2] - 48;
- byte.bits.b6 = pole[i][1] - 48;
- byte.bits.b7 = pole[i][0] - 48;
- s+=byte.byte;*/
- //-----------------------------
- }
- return s;
- }
- string decodestr2 (string str) {
- string s = "";
- string tmp;
- int last;
- for (int i = 0; i < str.size(); ++i) {
- if ( str[i] == '#' ) {
- tmp = str.substr( last, i - last );
- last = i+1;
- if (tmp.size() == 8) {
- //----------------------------
- char c = 0;
- unsigned char sub = 128;
- for (int j = 0; j < 8; ++j) {
- if (tmp[j] == '1') {
- c += sub;
- }
- sub = sub / 2;
- //sub = sub >> 1;
- }
- s+=c;
- //-----------------------------
- /*char c = 0;
- for (int j = 0; j < 8; ++j) {
- if (tmp[j] == '1') {
- c += pow(2,7-j);
- }
- }
- s+=c;*/
- //-----------------------------
- }
- }
- }
- return s;
- }
- int main(int argc, char** argv) {
- string vstup = "#01010101#011110#01010101#";
- cout << decodestr2 (vstup) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement