vendredi 24 juin 2016

Bit-Shift a string to decode save file C++

I am kinda trying to decode my save files. And I came up with this. But shifting the characters back from the save file doesn't seem to work properly. Only the numbers seem to recover correctly but all the other characters seem to change into strange symbols. As you can see I bit-shift the savecontent << 1 to the left, and on load I bit-shift the incoming line >> 1 to the right. But it doesn't work as I expected. Does bit-shift not work properly on strings?

void erGuiManager::SaveToFile(string filename) {
    ofstream ifs;
    ifs.open(filename, ios::binary);

    if (ifs.is_open())
    {
        string savecontent = "";
        for (int i = 0; i < guiItems.size(); i++) {
            if (dynamic_cast<erGuiBasicNode*>(guiItems[i])) {
                savecontent.append( dynamic_cast<erGuiBasicNode*>(guiItems[i])->save(true));
            }
        }
        for (int i = 0; i < savecontent.length(); i++) {
            savecontent[i] = savecontent[i] << 1;
        }
        ifs.write(savecontent.c_str(), strlen(savecontent.c_str()));
        ifs.close();
    }
    else {
        cout << "Error: Unable to open file " << filename;
    }
}

void erGuiManager::LoadFromFile(string filename) {
    string line;
    string out;
    ifstream myfile(filename);
    if (myfile.is_open())
    {
        while (getline(myfile, line))
        {
            for (int i = 0; i < line.length(); i++) {
                line[i] = line[i] >> 1;
            }
            out.append(PasteNodeFromString(line,true));
        }
        ConnectBezierLines(out);
        myfile.close();
    }
}

Aucun commentaire:

Enregistrer un commentaire