解码、小端、未压缩和浮点数组
我有来自实验的 Base64 编码数据。所以我试图逐步做的是:
- 从 base64 编码中检索字节(解码)
- 将字节转换为小尾数
- 从 (zlib) 解压缩字节
- 将字节数组转换为浮点数组
示例:
Dn3LQ3np4kOyxQJE20kDRBRuFkScZB5ENxEzRFa+O0THMz9EOQRCRFC1QkRYeUNEwXJJROfbSUScvE5EVDtVRK5PV0TLUWNE481lRHX7ZkSBBWpE9FVy RIFdeESkoHhEnid8RI1nfUSy4YBE/C2CRGKQg0RcR4RE54uEROUAhUTBWodErKyMRNsVkkRvUpJEukWURO58lkSqRZ1E2VauRPBTwEQf9cVE9BnKRA==
到目前为止我已经尝试过的
import os
import base64
import struct
s = 'Dn3LQ3np4kOyxQJE20kDRBRuFkScZB5ENxEzRFa+O0THMz9EOQRCRFC1QkRYeUNEwXJJROfbSUScvE5EVDtVRK5PV0TLUWNE481lRHX7ZkSBBWpE9FVyRIFdeESkoHhEnid8RI1nfUSy4YBE/C2CRGKQg0RcR4RE54uEROUAhUTBWodErKyMRNsVkkRvUpJEukWURO58lkSqRZ1E2VauRPBTwEQf9cVE9BnKRA=='
decode=base64.decodestring(s)
tmp_size=len(decode)/4
现在我正在尝试从这里将这些字节转换为小端。
我想在Python中进行下一步操作。
我正在尝试自己解决这个问题,但是这花费了太多时间。
谢谢!
I have Base64 encoded data from an experiment. So what I am trying to do in stepwise is:
- Retrieve bytes from base64 encoding (Decode it)
- Convert bytes to little-endian
- Decompress bytes from (zlib)
- Convert byte array to float array
Example:
Dn3LQ3np4kOyxQJE20kDRBRuFkScZB5ENxEzRFa+O0THMz9EOQRCRFC1QkRYeUNEwXJJROfbSUScvE5EVDtVRK5PV0TLUWNE481lRHX7ZkSBBWpE9FVyRIFdeESkoHhEnid8RI1nfUSy4YBE/C2CRGKQg0RcR4RE54uEROUAhUTBWodErKyMRNsVkkRvUpJEukWURO58lkSqRZ1E2VauRPBTwEQf9cVE9BnKRA==
What I have tried so far
import os
import base64
import struct
s = 'Dn3LQ3np4kOyxQJE20kDRBRuFkScZB5ENxEzRFa+O0THMz9EOQRCRFC1QkRYeUNEwXJJROfbSUScvE5EVDtVRK5PV0TLUWNE481lRHX7ZkSBBWpE9FVyRIFdeESkoHhEnid8RI1nfUSy4YBE/C2CRGKQg0RcR4RE54uEROUAhUTBWodErKyMRNsVkkRvUpJEukWURO58lkSqRZ1E2VauRPBTwEQf9cVE9BnKRA=='
decode=base64.decodestring(s)
tmp_size=len(decode)/4
Now I am trying to convert these byte to little endian from here.
I want to do the next operation in Python.
I am trying to figure it out myself but, it is taking too much time.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您的数据实际上并未被压缩。使用
struct.unpack_from()< 在循环中将数据读取为浮点数/code>
或使用
struct.unpack()
。如果数据被压缩,则将其解压缩。
It appears your data isn't actually compressed. Read the data as floats either in a loop using
struct.unpack_from()
or as one big structure usingstruct.unpack()
.If the data is compressed, decompress it.
字节排序仅适用于大于 1 字节的数据类型。因此,您不能只将字节列表转换为小端字节序。您需要了解字节列表中的内容。
32位整数是4个字节;如果你有16字节的数据。您可以将其“解压”为 4 个 32 位整数。
如果数据只是 ascii 文本,则字节顺序并不重要,这就是为什么您可以在大端和小端机器上读取完全相同的 ascii 文本文件。
以下是演示 struct.pack 和 struct.unpack 的示例:
查看文档以获取更多信息: http://docs.python.org/library/struct.html#format-strings
Byte ordering only applies to data types that are greater than 1 byte. So you can't just convert a list of bytes to little-endian. You need to understand what is in your list of bytes.
A 32-bit integer is 4 bytes; If you have 16 bytes of data. You could "unpack" that into 4 32-bit integers.
If the data is just ascii text the endianness doesn't matter, that's why you can read the exact same ascii text file on both big-endian and little-endian machines.
Here is an example demonstrating struct.pack and struct.unpack:
check out the documentation for more info: http://docs.python.org/library/struct.html#format-strings
看来您的下一步将是使用
struct
。类似这样的:此示例会将
decode
的前四个字节转换为浮点数。查看结构文档以获取有关格式字符串等的更多信息。Looks like your next step will be to use
struct
. Something like this:This example will turn the first four bytes of
decode
into a float. Check out the struct documentation for more info on format strings, etc.