我用matlab将单个值(val=2)写成24位数据,如下所示:
fid = fopen('.\t1.bin'), 'wb');
fwrite(fid, val, 'bit24', 0);
在bin查看器中,我可以看到数据(值2)存储为02 00 00。 我需要在python中将值作为单个整数读取。 我的以下代码不起作用:
^{pr2}$我也试过了
val = struct.unpack('>I',fileContent)
但它给出了错误:
unpack requires a string argument of length 4
我做错什么了?
谢谢
赛迪
在Python结构模块format characters中,
x
被定义为pad字节。这里的格式字符串表示读取3个字节,然后丢弃它们。在目前还没有一个格式说明符来处理24位数据,因此请自己构建一个:
通过访问单个字节,可以始终将整数转换为字节,反之亦然。只需要处理好endianness。在
下面的代码使用一个随机整数转换成24位二进制并返回。在
导入结构,随机
相关问题 更多 >
编程相关推荐