[英]How can I reorder this binary file
我有这两个二进制文件,它们是同一台机器上 java 代码和 C++ 代码的输出。 问题是字节顺序似乎不同(也许是小端与大端?)。 所以问题是:为什么这在同一台机器上不同? 以及如何以相同的字节顺序转换/查看文件以比较它们?
左侧的文件是使用以下方式实现的:
std::ofstream file("binary.bin", std::ios::binary);
for (const auto& row : vector) {
file.write(reinterpret_cast<const char*>(row.data()), row.size() * sizeof(float));
}
file.close();
在 Java(右)中:
File file = new File(path, "/" + "binary2.bin");
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(byteArray);
} catch (IOException e) {
e.printStackTrace();
}
谢谢
不幸的是,没有提供那么多信息。 看起来 4 个字节的块被颠倒了。
这可能取决于实现。 C++ 以一种方式存储,Java 以另一种方式存储。 但是由于我们看不到正在编写的程序,我们只能猜测。 我们也看不到文件的结构(1 字节数据或 2 字节数据或 4 字节数据或 8 字节数据块或带填充的结构或其他)
但是,至少我们可以重新排序 4 字节的二进制数据块。
那并不复杂。 我们打开手头的文件,读取std::vector
中的所有数据,然后关闭文件。
然后我们再次重新打开文件进行写入,这将覆盖之前在其中的所有内容。 该文件将为空。
我们使用算法库中的std::reverse
function 迭代std::vector
和 reverse 4 字节块中的源数据。
最后,我们将所有数据写回到文件中。
我起草了一些演示函数来创建测试文件并显示内容。
请参见:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <iterator>
#include <filesystem>
#include <algorithm>
const std::string testFileName{ "test.bin" };
// Simple program to write test data to a file
void writeTestFile() {
// Open file for writing and check, if it is open
if (std::ofstream ofs(testFileName,std::ios::binary); ofs) {
// Write 128 4-byte blocks
for (int i{}; i < 128; ++i)
for (int k{ 3 }; k >= 0; --k)
ofs << static_cast<unsigned char>(i * 4 + k);
}
else std::cerr << "\n*** Error. Could not open file '" << testFileName << "' for writing\n\n";
}
// Show binary file content on screen in hex
void showBinaryFile() {
// Open file and check, if it could be opened
if (std::ifstream ifs(testFileName, std::ios::binary); ifs) {
std::cout << "\n\n---------------------------------------------------------------\n";
unsigned int counterForLineBreak{};
// Read all data from file in a loop and display it
for (unsigned char c = ifs.get(); ifs; c = ifs.get()) {
std::cout << std::right << std::hex << std::setfill('0') << std::setw(2) << static_cast<unsigned int>(c) << ' ';
if (++counterForLineBreak % 16 == 0) std::cout << '\n';
}
}
else std::cerr << "\n*** Error. Could not open file '" << testFileName << "' for reading\n\n";
}
// Simply reverse 4 byte blocks of a binary file
// File size must be multiple of 4
void flip() {
// Open file for reading and check, if it could be opened
if (std::ifstream ifs(testFileName, std::ios::binary); ifs) {
// Read complete file into a std::vector
std::size_t size{ std::filesystem::file_size(testFileName) };
std::vector<unsigned char> content(size);
ifs.read(reinterpret_cast<char*>(content.data()), size);
// Close the file that we just read
ifs.close();
// Open file again. Now for writing. Check, if it could be opened
if (std::ofstream ofs(testFileName, std::ios::binary); ofs) {
// Reverse 4 byte blocks in std::vector
for (std::size_t i{}; i < size; i += 4)
std::reverse(content.data() + i, content.data() + std::min(i + 4, size));
// Write data back to file
ofs.write(reinterpret_cast<char*>(content.data()), size);
}
else std::cerr << "\n*** Error. Could not open file '" << testFileName << "' for writing\n\n";
}
else std::cerr << "\n*** Error. Could not open file '" << testFileName << "' for reading\n\n";
}
// Test program
int main() {
writeTestFile();
showBinaryFile();
flip();
showBinaryFile();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.