[英]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. 我看到的是我打开的第一个文件被正确读取,即file.get()返回有效的十六进制值。 An example of this would be the value 48ff in the file and get() retrieves 48 in hex or 72 as an int.
一个示例是文件中的值48ff,并且get()以十六进制形式检索48或将int检索为72。 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.
我还可以看到我的加密文件正确地以十六进制形式写入,但是当我去读取新创建的加密文件时,可以说其中的第一个值是81a4,我只得到第一个字符,“ 8”不是十六进制我期望并能够从我未创建的第一个文件中获取的值“ 81”。
ostream
's <<
operator writes formatted text , not raw data. ostream
的<<
操作符将写入格式化的文本 ,而不是原始数据。 For what you are attempting, you need to use the ostream::write()
method instead: 对于您要尝试的操作,您需要使用
ostream::write()
方法代替:
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). 您还会在
readFile()
函数中滥用eof()
readFile()
在尝试先读取某些内容之前,无法检查eof
)。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.