字节文件读取 我试图用C++中的fFrm读取一个字节文件(目标:二进制数据格式反序列化)。在HxD十六进制编辑器(bytes.dat)中,dat文件如下所示:
但是在将二进制文件读入char数组时出现了一些问题。。这里是一个mwe:字节文件读取 我试图用C++中的fFrm读取一个字节文件(目标:二进制数据格式反序列化)。在HxD十六进制编辑器(bytes.dat)中,dat文件如下所示:,c++,binary,fstream,C++,Binary,Fstream,但是在将二进制文件读入char数组时出现了一些问题。。这里是一个mwe: #include <iostream> #include <fstream> using namespace std; int main (){ ofstream outfile; outfile.open("bytes.dat", std::ios::binary); outfile << hex << (char) 0x77 << (char)
#include <iostream>
#include <fstream>
using namespace std;
int main (){
ofstream outfile;
outfile.open("bytes.dat", std::ios::binary);
outfile << hex << (char) 0x77 << (char) 0x77 << (char) 0x77 << (char) 0x07 \
<< (char) 0x9C << (char) 0x04 << (char) 0x00 << (char) 0x00 << (char) 0x41 \
<< (char) 0x49 << (char) 0x44 << (char) 0x30 << (char) 0x00 << (char) 0x00 \
<< (char) 0x04 << (char) 0x9C;
ifstream infile;
infile.open("bytes.dat", ios::in | ios::binary);
char bytes[16];
for (int i = 0; i < 16; ++i)
{
infile.read(&bytes[i], 1);
printf("%02X ", bytes[i]);
}
}
我做错了什么事。有些数组条目中怎么可能有4个字节?- 使用二进制数据(二进制文件格式等)时,最好使用无符号整数类型,以避免符号扩展名转换
- 正如在读取和写入二进制数据时所建议的那样,最好使用
和stream.read
函数(最好也是按块读取和写入)stream.write
- 如果需要存储固定二进制数据,请使用
或std::array
,如果需要从文件加载数据(std::vector
是默认值)std::vector
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
vector<unsigned char> bytes1{0x77, 0x77, 0x77, 0x07, 0x9C, 0x04, 0x00, 0x00,
0x41, 0x49, 0x44, 0x30, 0x00, 0x00, 0x04, 0x9C};
ofstream outfile("bytes.dat", std::ios::binary);
outfile.write((char*)&bytes1[0], bytes1.size());
outfile.close();
vector<unsigned char> bytes2(bytes1.size(), 0);
ifstream infile("bytes.dat", ios::in | ios::binary);
infile.read((char*)&bytes2[0], bytes2.size());
for (int i = 0; i < bytes2.size(); ++i) {
printf("%02X ", bytes2[i]);
}
}
#包括
#包括
#包括
使用名称空间std;
int main(){
向量字节1{0x77,0x77,0x77,0x07,0x9C,0x04,0x00,0x00,
0x41、0x49、0x44、0x30、0x00、0x00、0x04、0x9C};
流输出文件(“bytes.dat”,std::ios::binary);
outfile.write((char*)&bytes1[0],bytes1.size());
outfile.close();
向量bytes2(bytes1.size(),0);
ifstream-infle(“bytes.dat”,ios::in | ios::binary);
infle.read((char*)&bytes2[0],bytes2.size());
对于(int i=0;i
- 使用二进制数据(二进制文件格式等)时,最好使用无符号整数类型,以避免符号扩展名转换
- 正如在读取和写入二进制数据时所建议的那样,最好使用
和stream.read
函数(最好也是按块读取和写入)stream.write
- 如果需要存储固定二进制数据,请使用
或std::array
,如果需要从文件加载数据(std::vector
是默认值)std::vector
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
vector<unsigned char> bytes1{0x77, 0x77, 0x77, 0x07, 0x9C, 0x04, 0x00, 0x00,
0x41, 0x49, 0x44, 0x30, 0x00, 0x00, 0x04, 0x9C};
ofstream outfile("bytes.dat", std::ios::binary);
outfile.write((char*)&bytes1[0], bytes1.size());
outfile.close();
vector<unsigned char> bytes2(bytes1.size(), 0);
ifstream infile("bytes.dat", ios::in | ios::binary);
infile.read((char*)&bytes2[0], bytes2.size());
for (int i = 0; i < bytes2.size(); ++i) {
printf("%02X ", bytes2[i]);
}
}
#包括
#包括
#包括
使用名称空间std;
int main(){
向量字节1{0x77,0x77,0x77,0x07,0x9C,0x04,0x00,0x00,
0x41、0x49、0x44、0x30、0x00、0x00、0x04、0x9C};
流输出文件(“bytes.dat”,std::ios::binary);
outfile.write((char*)&bytes1[0],bytes1.size());
outfile.close();
向量bytes2(bytes1.size(),0);
ifstream-infle(“bytes.dat”,ios::in | ios::binary);
infle.read((char*)&bytes2[0],bytes2.size());
对于(int i=0;i
将字节
声明为无符号字符
数组或将字节[i]
转换为无符号字符
。tnx这解决了长FFFFFF问题。但是这些值不是十六进制编辑器使用的值,而是将运算符clare字节作为无符号字符
数组或将字节[i]
转换为无符号字符
。tnx这解决了长FFFFFF问题。但是这些值不是hex editorUse中的值,而是运算符中的值