如何解决我如何将字符串打包成3个字节的小端
在我将数据处理回3字节二进制文件之后,我试图将其导出,以便将其重新加载到程序中。
我已经附加了用于解压缩的代码。
但是我不知道编辑后如何重新打包。
有帮助吗?谢谢
import numpy as np
import matplotlib.pyplot as plt
import struct
def unpack_24bit(bytes):
return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16)
def swap8(i):
return struct.unpack("<i",struct.pack(">i",i))[0]
f=open('TEST_20190505_235640.0000000_Z_7_2.raw','rb')
print('============================');
print('PRINTING HEADER FOR FILE');
print('============================');
line = f.readline().decode('latin-1')
print(line);
scaleLine = '';
yZeroVoltOffsetLine = '';
nullingVoltageLine = '';
while('/DataInfo' not in line) :
line = f.readline().decode('latin-1')
if('<scale>' in line) :
scaleLine = line.split('<scale>')[1].split('</scale>')[0];
if('<y_zero_volt_offset>' in line) :
yZeroVoltOffsetLine = line.split('<y_zero_volt_offset>')[1].split('</y_zero_volt_offset>')[0];
if('<NullingVoltage>' in line) :
nullingVoltageLine=line.split('<NullingVoltage>')[1].split('</NullingVoltage>')[0];
print(line.replace('\n',''))
line = f.readline().decode('latin-1')
print(line);
print('============================');
scale = float(scaleLine);
offset= float(yZeroVoltOffsetLine);
nulling= float(nullingVoltageLine);
print('scale=',scale);
print('offset=',offset);
print('nulling=',nulling);
#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)
#loop over all values
for i in range(nvals) :
#a is first bit,b i second bit,c is third bit. Use unpack_24bit to convert to a signed 24 bit integer
a = ba[i*3+0];
b = ba[i*3+1];
c = ba[i*3+2];
# a = swap8(a)
# b = swap8(b)
# c = swap8(c)
#perform steps 1 and 2 to convert to voltage
dat[i] =((unpack_24bit([a,b,c])))
任何建议将不胜感激
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。