这怎么数据转换究竟表现如何? [英] How was this data conversion performed exactly?

查看:101
本文介绍了这怎么数据转换究竟表现如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,长话短说,我想'解压'的一些数据我自己,但我不能这样做。幸运的是我有解决方案,但我想不出方法。基本上,我有这个阵列中的数据在这里,

So, long story short, I am trying to 'unpack' some data myself, but I am not able to do so. Fortunately I have the solution, but I cannot figure out the method. Basically, I have this array of data here,

[16 130 164 65 103 136 209 64 19 36 185 190 83]

[16 130 164 65 103 136 209 64 19 36 185 190 83]

而据我所知,当通过'FFFB ,我得到解包:

, and I am told that when 'unpacked' via 'fffB', I get:

[20.56350708,6.54790068,-0.36160335,83]

[ 20.56350708,   6.54790068,  -0.36160335,  83]

我知道一个事实,这个解决方案是正确的,但我不知道我们是如何实现它。

I know for a fact that this solution is correct, but I am not sure how we attained it.

这里的上下文是输入数组是解包,使用Python的命令,像这样:

The context here is that the input array was 'unpacked', using Python's command as so:

struct.unpack_from('fffB', input)

但尝试,因为我可以,我无法理解这里的具体操作。

However try as I may, I am unable to understand the exact operation here.

推荐答案

首先,你需要的号码列表转换为字符串,因为那是什么的 解压 预计:

First you need to convert the list of numbers to a string since that's what unpack expects:

根据格式字符串FMT从缓冲区缓存(由包psumably包装$ P $(FMT,...))打开。其结果是,即使它包含一个项目的元组。该缓冲区的字节必须由格式所需要的尺寸,大小一致通过calcsize反映()。

Unpack from the buffer buffer (presumably packed by pack(fmt, ...)) according to the format string fmt. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes must match the size required by the format, as reflected by calcsize().

在列表数可转换为字符 CHR ,一旦你加入在一起,你有一个可以给解压

Number in the list can be converted to characters with chr and once you join them together you have input that can be given to unpack:

import struct

d = [16, 130, 164, 65, 103, 136, 209, 64, 19, 36, 185, 190, 83]
s = ''.join(chr(x) for x in d) # s = bytearray(d) on Python 3

struct.unpack('fffB', s) # (20.563507080078125, 6.547900676727295, -0.36160334944725037, 83)

格式字符串 FFFB 告诉解压来提取1个字节的每和一个无符号字符大小为4个字节3彩车。在总共13个字节被提取,你的数据的长度相匹配。格式字符确切的规格可以从 Python文档找到。

Format string fffB tells unpack to extract three floats of 4 bytes each and one unsigned character size of 1 byte. In total 13 bytes are extracted which matches the length of your data. The exact specification of format characters can be found from Python documentation.

请注意,上面的例子只是与Python 2.x的工作,Python的3​​.x的,你需要将列表转换的 字节组 ,而不是字符串。

Note that above example only works with Python 2.x, on Python 3.x you need to convert the list to bytearray instead of string.

这篇关于这怎么数据转换究竟表现如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆