在Python中将二进制文件转换为ASCII

问题描述 投票:2回答:3

我有一堆二进制文件,其中包含以下格式的数据:

i\\xffhh\\xffhh\\xffhh\\xffih\\xffhh\\xffhh\\xffhh\\xffhh\\xffhi\\xffii\\xffjj\\xffjj\\xffjj\\xffjk\\xffkk\\xffkk\\xffkl\\xffll\\xffmm\\xffmn\\xffnn\\xffon\\xffno\\xffop\\xffop\\xffpp\\xffqq\\xffrq\\xffrs\\xffst\\xfftt\\xfftt\\xffuv\\xffvu\\xffuv\\xffvv\\xffvw\\xffwx\\xffwx\\xffxy\\xffyy\\xffyz\\xffz{\\xffz{\\xff||\\xff}|\\xff~}\\xff}}\\xff~~\\xff~~\\xff~\\x7f\\xff\\x7f\\x7f\\xff\\x7f\\x7f\\xff\\x7f\\x7f\\xff\\x80\\x80\\xff\\x80\\x81\\xff\\x81\\x80\\xff\\x81\\x81\\xff\\x81\\x82\\xff\\x82\\x82\\xff\\x82\\x82\\xff\\x82\\x83\\xff\\x83\\x83\\xff\\x83\\x83\\xff\\x83\\x84\\xff\\x83\\x84\\xff\\x84\\x85\\xff\\x85\\x85\\xff\\x86\\x85\\xff\\x86\\x87\\xff\\x87\\x87\\xff\\x87\\x87\\xff\\x88\\x87\\xff\\x88\\x89\\xff\\x88\\x89\\xff\\x89\\x8a\\xff\\x89\\x8a\\xff\\x8a\\x8b\\xff\\x8b\\x8b\\xff\\x8b\\x8c\\xff\\x8d\\x8d\\xff\\x8d\\x8d\\xff\\x8e\\x8e\\xff\\x8e\\x8f\\xff\\x8f\\x8f

这些应该是人走路时的压力传感器读数,所以我假设它们是数字,但是我想将它们转换为ascii,所以我对它们是什么有所了解。 如何转换它们? 他们目前使用什么格式?

编辑:链接到此处提供的文件( Link

python binary ascii decoding
3个回答
4
投票

您不能仅通过打开二进制文件来猜测格式。 您将必须获取有关特定压力传感器读数的数据存储方式的信息。

当然,当您知道格式后,很容易以二进制模式读取文件,然后从中获取所有有意义的数据

FILE = open(filename,"rb")
FILE.read(numBytes)

3
投票

我绝对感到震惊和震惊,对所有的华夫饼干都没有感到惊讶,例如“您有像hh这样的字母,不应该是十六进制数字的一部分”和“它们似乎从第一个\\ x7f开始就有意义了” 。 没有人看到任何repr()输出吗?

下面显示了它可能如何以这种方式结束,而忽略了似乎只是杂音的\\xff

>>> pressure = [120,121,122,123,124,125,126,127,128,129,130,131]
>>> import struct
>>> some_bytes = struct.pack("12B", *pressure)
>>> print repr(some_bytes)
'xyz{|}~\x7f\x80\x81\x82\x83'
>>>

因此,让我们尝试从文件中恢复工作:

>>> guff = open('your_file.bin', 'rb').read()
>>> cleaned = guff.replace("\xff", "")
>>> cleaned
'ihhhhhhihhhhhhhhhhiiijjjjjjjkkkkkklllmmmnnnonnoopopppqqrqrsstttttuvvuuvvvvwwxwx
xyyyyzz{z{||}|~}}}~~~~~\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x80\x80\x80\x81\x81\x80\x81\
x81\x81\x82\x82\x82\x82\x82\x82\x83\x83\x83\x83\x83\x83\x84\x83\x84\x84\x85\x85\
x85\x86\x85\x86\x87\x87\x87\x87\x87\x88\x87\x88\x89\x88\x89\x89\x8a\x89\x8a\x8a\
x8b\x8b\x8b\x8b\x8c\x8d\x8d\x8d\x8d\x8e\x8e\x8e\x8f\x8f\x8f'
# Note that lines wrap at column 80 in a Windows "Command Prompt" window ...
>>> pressure = [ord(c) for c in cleaned]
>>> pressure
[105, 104, 104, 104, 104, 104, 104, 105, 104, 104, 104, 104, 104, 104, 104, 104,
 104, 104, 105, 105, 105, 106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107,
 107, 107, 108, 108, 108, 109, 109, 109, 110, 110, 110, 111, 110, 110, 111, 111,
 112, 111, 112, 112, 112, 113, 113, 114, 113, 114, 115, 115, 116, 116, 116, 116,
 116, 117, 118, 118, 117, 117, 118, 118, 118, 118, 119, 119, 120, 119, 120, 120,
 121, 121, 121, 121, 122, 122, 123, 122, 123, 124, 124, 125, 124, 126, 125, 125,
 125, 126, 126, 126, 126, 126, 127, 127, 127, 127, 127, 127, 127, 128, 128, 128,
 129, 129, 128, 129, 129, 129, 130, 130, 130, 130, 130, 130, 131, 131, 131, 131,
 131, 131, 132, 131, 132, 132, 133, 133, 133, 134, 133, 134, 135, 135, 135, 135,
 135, 136, 135, 136, 137, 136, 137, 137, 138, 137, 138, 138, 139, 139, 139, 139,
 140, 141, 141, 141, 141, 142, 142, 142, 143, 143, 143]
>>>

您仍然需要阅读设备的文档,以找出将0-254值乘以的比例因子是多少。

您会注意到,派生的数字每次都会更改+1、0或-1。 这与每个读数只有1个字节,而不是每个读数两个或更多字节的假设很吻合。

另一个想法: \\xff可能是一个开始或结束\\xff ,每个周期报告两个值(开始,停止)或(传感器-A,传感器-B)。


0
投票

第一部分看起来很奇怪。 通常,像\\ x8e这样的数字只是一个十六进制字节的代码,除了在第一部分中您有类似hh之类的字母,不应该是十六进制数字的一部分。

但是对于第二部分,您可以执行以下操作:

hex_list = r"\x7f\xff\x7f\x7f\xff\x7f\x7f\xff\x7f\x7f\xff\x80\x80\xff\x80\x81\xff\x81\x80\xff\x81\x81\xff\x81\x82\xff\x82\x82\xff\x82\x82\xff\x82\x83\xff\x83\x83\xff\x83\x83\xff\x83\x84\xff\x83\x84\xff\x84\x85\xff\x85\x85\xff\x86\x85\xff\x86\x87\xff\x87\x87\xff\x87\x87\xff\x88\x87\xff\x88\x89\xff\x88\x89\xff\x89\x8a\xff\x89\x8a\xff\x8a\x8b\xff\x8b\x8b\xff\x8b\x8c\xff\x8d\x8d\xff\x8d\x8d\xff\x8e\x8e\xff\x8e\x8f\xff\x8f\x8f"
int_list =  [int(hex,16) for hex in hex_list.replace('\\', ';0').split(';') if hex != '']

请注意,除255(\\ xff)外,您总是得到127到143之间的数字。

© www.soinside.com 2019 - 2024. All rights reserved.