我一直在這一整天,我似乎無法找到一個解決方案:(Python包裝int32但不起作用?
我有一個頭文件我想創建一個文件(我解析一個Python的obj文件輸出到二進制在我的C++遊戲引擎)被加載了數據。
這裏是網格報頭
struct MeshHeader
{
unsigned short _vertex_size;
uint32 _vertex_count;
unsigned short _index_buffer_count;
short _position_offset;
unsigned short _position_component_count;
short _uv_offset;
unsigned short _uv_component_count;
short _normal_offset;
unsigned short _normal_component_count;
short _tangent_offset;
unsigned short _tangent_component_count;
short _binormal_offset;
unsigned short _binormal_component_count;
short _colour_offset;
unsigned short _colour_component_count;
};
的C++定義在哪裏UINT32基本上是從uint32_t的一個typedef從stdint.h .... 所以從這個角度來看,前三個成員變量分別是2個字節,4個字節,2個字節,是的?
這是我讀入結構
fread(&_header, sizeof(MeshHeader), 1, f);
_vertex_size正確gets設置到56,但如果我改變它的數據類型爲UINT16(無符號短)_vertex_count被設置爲65536。它被正確設置到36.但爲什麼? 我正在使用pack("<I")
函數(知道我的機器是小端)。
這是蟒蛇
f.write(pack('<H', self._vertex_size))
f.write(pack('<I', self._vertex_count))
f.write(pack('<H', self._index_buffer_count))
f.write(pack('<h', self._position_offset))
f.write(pack('<H', self._position_component_count))
f.write(pack('<h', self._uv_offset))
f.write(pack('<H', self._uv_component_count))
f.write(pack('<h', self._normal_offset))
f.write(pack('<H', self._normal_component_count))
f.write(pack('<h', self._tangent_offset))
f.write(pack('<H', self._tangent_component_count))
f.write(pack('<h', self._binormal_offset))
f.write(pack('<H', self._binormal_component_count))
f.write(pack('<h', self._colour_offset))
f.write(pack('<H', self._colour_component_count))
繼struct.pack功能(https://docs.python.org/2/library/struct.html)的規格收拾了代碼... H是無符號short(2個字節)我是無符號整數(4個字節)和h是一個短的(2個字節),它與我在C MeshHeader類中指定的完全一致,不是嗎?
過去幾個小時我一直拉出我的頭髮(而且我沒有太多它剩下的!)。關於可能發生什麼的任何建議?
這裏是頭文件的崇高文本快照3 BTW http://gyazo.com/e15942753819e695617390129e6aa879
根據您的C++代碼的編譯方式,可能會在結構成員之間添加額外的字節,以便在32位或64位邊界上對齊它們,使計算機能夠比非-aligned。可能有一個編譯器選項來禁用這個,像'-StructPack' ... – martineau
嘿謝謝你的回覆!我已經使用Visual Studio 2013社區版對32位和64位版本進行了試用。兩者都產生了我得到的相同結果。 –
更改體系結構,特別是更大的單詞大小,可能無法解決此問題。您需要明確修改結構的打包方式。 –