简体   繁体   English

在不了解字符串的情况下使用struct.unpack()

[英]Using struct.unpack() without knowing anything about the string

I need to parse a big-endian binary file and convert it to little-endian. 我需要解析一个big-endian二进制文件,并将其转换为little-endian。 However, the people who have handed the file over to me seem unable to tell me anything about what data types it contains, or how it is organized — the only thing they know for certain is that it is a big-endian binary file with some old data. 但是,将文件交给我的人似乎无法告诉我有关文件包含的数据类型或组织方式的任何信息–他们唯一可以肯定的是,它是一个大尾数二进制文件, 其中包含一些旧数据。 The function struct.unpack(), however, requires a format character as its first argument. 但是,函数struct.unpack()需要格式字符作为其第一个参数。

This is the first line of the binary file: 这是二进制文件的第一行:

import binascii
path = "BC2003_lr_m32_chab_Im.ised"            
with open(path, 'rb') as fd:
    line = fd.readline()
    print binascii.hexlify(line)

a0040000dd0000000000000080e2f54780f1094840c61a4800a92d48c0d9424840a05a48404d7548e09d8948a0689a48e03fad48a063c248c01bda48c0b8f448804a0949100b1a49e0d62c49e0ed41499097594900247449a0a57f4900d98549b0278c49a0c2924990ad9949a0eba049e080a8490072b049c0c2b849d077c1493096ca494022d449a021de49a099e849e08ff349500a a0040000dd0000000000000080e2f54780f1094840c61a4800a92d48c0d9424840a05a48404d7548e09d8948a0689a48e03fad48a063c248c01bda48c0b8f448804a0949100b1a49e0d62c49e0ed41499097594900247449a0a57f4900d98549b0278c49a0c2924990ad9949a0eba049e080a8490072b049c0c2b849d077c1493096ca494022d449a021de49a099e849e08ff349500a

Is it possible to change the endianness of a file without knowing anything about it? 是否有可能在不了解文件的情况下更改文件的字节序?

You cannot do this without knowing the datatypes. 如果不知道数据类型,则无法执行此操作。 There is little point in attempting to do so otherwise. 否则尝试没有任何意义。

Even if it was a homogeneous sequence of one datatype, you'd still need to know what you are dealing with; 即使它是一个数据类型的同质序列,您仍然需要知道您要处理的内容。 flipping the byte order in double values is very different from short integers. 将字节顺序翻转为double精度值与short整数有很大不同。

Take a look at the formatting characters table ; 看一下格式化字符表 anything with a different byte size in it will result in a different set of bytes being swapped; 字节大小不同的任何内容都将导致交换不同的字节集; for double values, you need to reverse the order of every 8 bytes, for example. 对于double值,例如,您需要反转每8个字节的顺序。

If you know what data should be in the file, then at least you have a starting point; 如果您知道文件中应该包含哪些数据,那么至少您有一个起点。 you'd have to puzzle out how those values fit into the bytes given. 您将不得不困惑那些值如何适合给定的字节。 It'll be a puzzle, but with a target set of values you can build a map of the datatypes contained, then write a byte-order adjustment script. 这将是一个难题,但是有了目标值集,您可以构建包含的数据类型的映射, 然后编写字节顺序调整脚本。 If you don't even have that, best not to start as the task is impossible to achieve. 如果您什至没有,最好不要开始,因为不可能完成任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM