0

我正在尝试遍历一个非常大的有符号 24 位小端二进制文件以转换为十进制

这是我目前正在使用的代码

def unpack_24bit(bytes):
    return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16)

f=open('1.raw','rb')

#Read in the data as a byte array (8-bit array)
ba = bytearray(f.read())[enter image description here][1]
length = len(ba)
print('N-bytes=',length)
#Since the data is in 24bits, convert three bits at a time to a 24 signed bit
nvals = int(length/3);
print('N-recordings for 24bits (3-byte data)=',nvals)
dat = np.zeros(nvals)

for i in range(0,nvals):
    dat[i] = (unpack_24bit(struct.unpack('<bbb', ba[3*i:3+i*3])))

它给出的结果可以在这里看到 [1]:https ://i.stack.imgur.com/FfWPn.png 它不太正确

它应该显示为方波

有什么建议么?

4

1 回答 1

0

想通了,会在这里发布,以防有​​人有类似的问题。

前 3 个字节是无符号的,最后一个字节是有符号的

def unpack_24bit(bytes):
    return bytes[0] | (bytes[1] << 8) | (bytes[2]) << 16

f=open('TX_Testing_201202_20201202_044904.0000000_Other_1_1 - Copy.raw','rb')

#Read in the data as a byte array (8-bit array)
ba = bytearray(f.read())
length = len(ba)
print('N-bytes=',length)
#Since the data is in 24bits, convert three bits at a time to a 24 signed bit
nvals = int(length/3);
print('N-recordings for 24bits (3-byte data)=',nvals)
dat = np.zeros(nvals)

for i in range(0,nvals):
    dat[i] = (unpack_24bit(struct.unpack('<BBb', ba[3*i:3+i*3]))*scale+offset+nulling)
于 2021-01-06T08:05:04.157 回答