简体   繁体   中英

C++ having issues reading and writing Hex to file

so I am trying to read from a hex file, modify the hex value, write the new hex value to a new file. Then open up the new file, modify the hex again and rewrite it to a third file. I am doing very simple encryption on the hex values.

the function I use to read is as fallows:

vector<unsigned char> readFile(istream& file){
    vector<unsigned char> returnValue;
    //grab first 32 values from infile
    for(int i = 0; i < 32 && !file.eof(); i++) {
        returnValue.push_back(file.get());
    }
    return returnValue;
}

The function I use to write is as fallows:

void WriteVectorU8(ostream &file, vector<u8> bytes, bool isEncrypt){
    for(int i = 0; i < bytes.size(); i++){
        u8 byteToWrite = isEncrypt ? encrypt(bytes[i] , curKey[keyPointer]) : decrypt(bytes[i], curKey[keyPointer]);
        incKeyPointer();
        if(i != 0 && i%2 == 0){
            file << " ";
        }
        file << hex << setw(2) << setfill('0') << int(byteToWrite);

    }
    file << endl;
}

Here is how I open the files:

ofstream outFile;
outFile.open("tempName.bin", ios::binary);

What I am seeing is the first file I open is being read correctly, IE the file.get() is returning a valid hex value. An example of this would be the value 48ff in the file and get() retrieves 48 in hex or 72 as an int. I can also see that my encrypted file is bing written correctly in hex, however when i go to read my newly created encrypted file lets say the first value in it is 81a4 I am only getting the first char, '8' not an the hex value '81' that I was expecting and was able to get from the first file i did not create.

ostream 's << operator writes formatted text , not raw data. For what you are attempting, you need to use the ostream::write() method instead:

void WriteVectorU8(ostream &file, vector<u8> bytes, bool isEncrypt){
    for(int i = 0; i < bytes.size(); i++){
        u8 byteToWrite = isEncrypt ? encrypt(bytes[i] , curKey[keyPointer]) : decrypt(bytes[i], curKey[keyPointer]);
        incKeyPointer();
        file.write((char*)&byteToWrite, 1);
    }
}

You are also misusing eof() in your readFile() function (you can't check for eof before attempting to read something first). It should be more like this instead:

vector<unsigned char> readFile(istream& file){
    vector<unsigned char> returnValue;
    //grab first 32 values from infile
    for(int i = 0; i < 32; i++) {
        char ch = file.get();
        if (!file) break;
        returnValue.push_back(ch);
    }
    return returnValue;
}

Or:

vector<unsigned char> readFile(istream& file){
    vector<unsigned char> returnValue;
    //grab first 32 values from infile
    char ch;
    for(int i = 0; (i < 32) && (file.get(ch)); i++) {
        returnValue.push_back(ch);
    }
    return returnValue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM