简体   繁体   English

在python中打开由ac程序编写为1和0的16位整数的二进制文件

[英]open binary file of 16 bit integers written by a c program as 1s and 0s in python

I have a bitstream file that is comprised of several lines.我有一个由几行组成的比特流文件。 The c program executable that produces the file outputs the file as a series of "short int" which are actually 16 bit integers.生成文件的 c 程序可执行文件将文件输出为一系列“短整数”,实际上是 16 位整数。 When I open the file in notepad I get the following first few lines:当我在记事本中打开文件时,我得到以下前几行:

7E1755EB7909DAC8FF4117BDAA0E86EBD1A8
1C3D47DD6606D812E8862D347288C3A251EB
16D7D02AD908E0083C142C107AB916C55BE0

I need to be able to open this file in Python and convert it to 1s and 0s that represent the original "short int" or in other words an array of 1s and 0s.我需要能够在 Python 中打开此文件并将其转换为代表原始“短整数”的 1 和 0,或者换句话说,是一个由 1 和 0 组成的数组。 I think I might also have an issue with the "\\n" meaning new line when I read in the file.我想当我读入文件时,“\\n”表示换行也可能有问题。

I've tried the following code to see which method works best:我已经尝试了以下代码来查看哪种方法效果最好:

import struct

filePathC = "C:\\Working\\Vocoder Sims\\ofile.chan"

fileC = open(filePathC, "rb")

with fileC:

    byteC = fileC.read(8)

    binaryC1 = bin(int(byteC,16))
    binaryC2 = struct.unpack("h" * (len(byteC)/2),byteC)

    print binaryC1
    print binaryC2

The result for when I only read in the first 8 bytes is:当我只读取前 8 个字节时的结果是:

0b1111110000101110101010111101011
(17719, 14129, 13621, 16965)

The issue with the first result is that I should be getting 64 1s and 0s and the problem with the second is that it "tuple" instead of an array of 1s and 0s and I don't believe the integers are 16 bit based.第一个结果的问题是我应该得到 64 个 1 和 0,而第二个结果的问题是它是“元组”而不是 1 和 0 的数组,我不相信整数是基于 16 位的。 They look more like 15 bit but I'm not sure.它们看起来更像是 15 位,但我不确定。

Thanks in advance for any help.在此先感谢您的帮助。

Assuming you have binary data that just happens to decode as ascii hex, you could read the file into a python array .假设您有刚好解码为 ascii 十六进制的二进制数据,您可以将文件读入 python array Its not much different than your second example of unpacking into a tuple except that its faster and has a lower memory footprint.它与解包成元组的第二个示例没有太大不同,只是它更快并且内存占用更少。 Depending on what you want to do next, it would be reasonable to read into a numpy array instead.根据您接下来要做什么,读入一个numpy数组是合理的。

import os
import array

#filePathC = "C:\\Working\\Vocoder Sims\\ofile.chan"
filePathC = "test.bin"

count = os.stat(filePathC).st_size / 2
with open(filePathC, 'rb') as fp:
    binaryC3 = array.array("h")
    binaryC3.fromfile(fp, count)

print binaryC3
print bin(binaryC3[0])

For your example file, this gave me对于您的示例文件,这给了我

array('h', [17719, 14129, 13621, 16965, 14647, 14640, 16708, 14403, 17990, 12596, 14129, 17474, 16705, 17712, 13880, 16965, 12612, 14401, 12554, 13123, 13380, 17463, 13892, 12342, 17462, 12600, 17714, 14392, 12854, 13124, 14132, 14386, 17208, 16691, 13618, 17713, 2626, 13873, 14148, 12356, 16690, 14660, 14384, 12357, 14384, 17203, 13361, 17202, 12337, 16695, 14658, 13873, 13635, 16949, 12357])
0b100010100110111

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

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