How could I parse binary data files?
[user01@centos66 binaryTest]$ od -bc int32.bin
0000000 000 000 000 000 001 000 000 000 002 000 000 000 003 000 000 000
\0 \0 \0 \0 001 \0 \0 \0 002 \0 \0 \0 003 \0 \0 \0
0000020
[user01@centos66 binaryTest]$ od -bc double.bin
0000000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 360 077
\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 360 ?
0000020 000 000 000 000 000 000 000 100 000 000 000 000 000 000 010 100
\0 \0 \0 \0 \0 \0 \0 @ \0 \0 \0 \0 \0 \0 \b @
0000040
[user01@centos66 binaryTest]$ cat a.cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream ofsInt32(“int32.bin”, ios::binary);
for(int a = 0; a < 4; ++a) {
ofsInt32.write(reinterpret_cast<const char *>(&a), sizeof(a));
}
ofsInt32.close();
ofstream ofsDouble(“double.bin”, ios::binary);
for(double a = 0; a < 4; ++a) {
ofsDouble.write(reinterpret_cast<const char *>(&a), sizeof(a));
}
ofsDouble.close();
return 0;
}