【发布时间】:2017-11-25 23:04:12
【问题描述】:
我正在尝试读取在 C++ 中生成的具有以下结构的已知数据类型的二进制文件:
uint64_t shot;
uint32_t status;
double easting, northing, altitude;
uint32_t zoneNumber;
char zoneLetter;
float vNorth, vEast, vDown;
float qw, qx, qy, qz;
float acel_x, acel_y, acel_z, gyro_x, gyro_y, gyro_z;
float rollStdDev, pitchStdDev, yawStdDev;
float northingStdDev, eastingStdDev, altitudeStdDev;
float vNorthStdDev, vEastStdDev, vDownStdDev;
这里发生的唯一奇怪是“区域字母”是一个 1 字节的字符,后面有 3 个字节的填充。所以字符应该从第 40 字节开始,然后 vNorth 应该从第 44 字节开始。
这是我尝试使用 numpy 使用 python 读取这个二进制文件。我不确定我是否正确转换了 c++ 双打,而且我很确定我的问题来自读取字符。
问题在于,在第一次迭代镜头和状态读取正确,但此后读取的所有内容对于实际数据的外观没有任何意义。所以我认为从字节 12-44 读取字符或重复读取存在问题。
dt2 = np.dtype([('ShotNum', np.uint64), ('Status', np.uint32), ('easting', np.float_),\
('northing', np.float_),('alt', np.float_), ('Znum', np.uint32),('letter', np.character),\
('vnorth', np.float32),('veast', np.float32),('vdown', np.float32),\
('qw', np.float32),('qx', np.float32),('qy', np.float32),('qz', np.float32),\
('acX', np.float32),('acY',np.float32),('acZ', np.float32),('gyX', np.float32),\
('gyY', np.float32),('gyZ', np.float32),('rollSTD', np.float32),('pitSTD', np.float32),\
('yawSTD', np.float32),('northSTD', np.float32),('eastSTD', np.float32),('altSTD', np.float32),\
('vnorthSTD', np.float32),('veastSTD', np.float32),('vDownSTD', np.float32)])
这是 c++ 显式结构
[thread: 892] mainwindow.cpp(65): Sizeof NavigationSolution: 136 bytes
[thread: 892] mainwindow.cpp(66): shot
[thread: 892] mainwindow.cpp(67): bytes: 8
[thread: 892] mainwindow.cpp(68): offset: 0
[thread: 892] mainwindow.cpp(69): status
[thread: 892] mainwindow.cpp(70): bytes: 4
[thread: 892] mainwindow.cpp(71): offset: 8
[thread: 892] mainwindow.cpp(72): easting
[thread: 892] mainwindow.cpp(73): bytes: 8
[thread: 892] mainwindow.cpp(74): offset: 16
[thread: 892] mainwindow.cpp(75): northing
[thread: 892] mainwindow.cpp(76): bytes: 8
[thread: 892] mainwindow.cpp(77): offset: 24
[thread: 892] mainwindow.cpp(78): altitude
[thread: 892] mainwindow.cpp(79): bytes: 8
[thread: 892] mainwindow.cpp(80): offset: 32
[thread: 892] mainwindow.cpp(81): zoneNumber
[thread: 892] mainwindow.cpp(82): bytes: 4
[thread: 892] mainwindow.cpp(83): offset: 40
[thread: 892] mainwindow.cpp(84): zoneLetter
[thread: 892] mainwindow.cpp(85): bytes: 1
[thread: 892] mainwindow.cpp(86): offset: 44
[thread: 892] mainwindow.cpp(87): vNorth
[thread: 892] mainwindow.cpp(88): bytes: 4
[thread: 892] mainwindow.cpp(89): offset: 48
[thread: 892] mainwindow.cpp(90): vEast
[thread: 892] mainwindow.cpp(91): bytes: 4
[thread: 892] mainwindow.cpp(92): offset: 52
[thread: 892] mainwindow.cpp(93): vDown
[thread: 892] mainwindow.cpp(94): bytes: 4
[thread: 892] mainwindow.cpp(95): offset: 56
…等
【问题讨论】:
-
“我很确定我的问题……” 你还没有解释你的问题。这使得帮助您解决它们变得困难。 :)
-
我认为
('letter', np.character)不会起作用。您需要考虑所有四个字节(字母和填充)。尝试将其替换为('letter', 'S1'), ('padding', 'S3')。 -
为了更清楚一点而编辑了问题。我会试一试沃伦
-
我尝试了您的解决方案 Warren,但输出仍然没有意义。我不确定 C++ double 是否是 python numpy float_
-
二进制文件究竟是如何创建的?您显示的 C++ 变量是否实际上是
struct中的字段,并且struct是作为单个内存块写出的吗?还是每个字段单独写?如果是前者,请注意编译器可能会向结构添加填充(可能是为了改善内存对齐)。例如,编译器可能在status字段之后添加了四个字节,以使easting对齐八字节。
标签: python c++ numpy binaryfiles