解码、小端、未压缩和浮点数组

发布于 2024-12-05 12:50:31 字数 916 浏览 0 评论 0原文

我有来自实验的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我很OK 2024-12-12 12:50:31

看起来您的数据实际上并未被压缩。使用 struct.unpack_from()< 在循环中将数据读取为浮点数/code>或使用 struct.unpack()

import base64
import struct

encoded = 'Dn3LQ3np ... 9BnKRA=='

# decode the string
data = base64.standard_b64decode(encoded)

# ensure that there's enough data for 32-bit floats
assert len(data) % 4 == 0

# determine how many floats there are
count = len(data) // 4

# unpack the data as floats
result = struct.unpack('<{0}f'.format(count), # one big structure of `count` floats
                       data)                  # results returned as a tuple

如果数据被压缩,则将其解压缩。

import zlib

decompressed = zlib.decompress(data)

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 using struct.unpack().

import base64
import struct

encoded = 'Dn3LQ3np ... 9BnKRA=='

# decode the string
data = base64.standard_b64decode(encoded)

# ensure that there's enough data for 32-bit floats
assert len(data) % 4 == 0

# determine how many floats there are
count = len(data) // 4

# unpack the data as floats
result = struct.unpack('<{0}f'.format(count), # one big structure of `count` floats
                       data)                  # results returned as a tuple

If the data is compressed, decompress it.

import zlib

decompressed = zlib.decompress(data)
空袭的梦i 2024-12-12 12:50:31

将字节转换为小尾数

字节排序仅适用于大于 1 字节的数据类型。因此,您不能只将字节列表转换为小端字节序。您需要了解字节列表中的内容。

32位整数是4个字节;如果你有16字节的数据。您可以将其“解压”为 4 个 32 位整数。

如果数据只是 ascii 文本,则字节顺序并不重要,这就是为什么您可以在大端和小端机器上读取完全相同的 ascii 文本文件。

以下是演示 struct.pack 和 struct.unpack 的示例:

#!/usr/bin/env python2.7
import struct
# 32-bit unsigned integer
# base 10       2,864,434,397
# base 16       0xAABBCCDD
u32 = 0xAABBCCDD
print 'u32 =', u32, '(0x%x)' % u32
# big endian    0xAA 0xBB 0xCC 0xDD
u32be = struct.pack('>I', u32)
bx = [byte for byte in struct.unpack('4B', u32be)]
print 'big endian packed', ['0x%02x' % x for x in bx]
assert bx == [0xaa, 0xbb, 0xcc, 0xdd]
# little endian 0xDD 0xCC 0xBB 0xAA
u32le = struct.pack('<I', u32)
lx = [byte for byte in struct.unpack('4B', u32le)]
print 'little endian packed', ['0x%02x' % x for x in lx]
assert lx == [0xdd, 0xcc, 0xbb, 0xaa]
# 64-bit unsigned integer
# base 10       12,302,652,060,662,200,000
# base 16       0xAABBCCDDEEFF0011
u64 = 0xAABBCCDDEEFF0011L
print 'u64 =', u64, '(0x%x)' % u64
# big endian    0xAA 0xBB 0xCC 0xDD 0xEE 0xFF 0x00 0x11
u64be = struct.pack('>Q', u64)
bx = [byte for byte in struct.unpack('8B', u64be)]
print 'big endian packed', ['0x%02x' % x for x in bx]
assert bx == [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11]
# little endian 0x11 0x00 0xFF 0xEE 0xDD 0xCC 0xBB 0xAA
u64le = struct.pack('<Q', u64)
lx = [byte for byte in struct.unpack('8B', u64le)]
print 'little endian packed', ['0x%02x' % x for x in lx]
assert lx == [0x11, 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa]

查看文档以获取更多信息: http://docs.python.org/library/struct.html#format-strings

Convert bytes to little-endian

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:

#!/usr/bin/env python2.7
import struct
# 32-bit unsigned integer
# base 10       2,864,434,397
# base 16       0xAABBCCDD
u32 = 0xAABBCCDD
print 'u32 =', u32, '(0x%x)' % u32
# big endian    0xAA 0xBB 0xCC 0xDD
u32be = struct.pack('>I', u32)
bx = [byte for byte in struct.unpack('4B', u32be)]
print 'big endian packed', ['0x%02x' % x for x in bx]
assert bx == [0xaa, 0xbb, 0xcc, 0xdd]
# little endian 0xDD 0xCC 0xBB 0xAA
u32le = struct.pack('<I', u32)
lx = [byte for byte in struct.unpack('4B', u32le)]
print 'little endian packed', ['0x%02x' % x for x in lx]
assert lx == [0xdd, 0xcc, 0xbb, 0xaa]
# 64-bit unsigned integer
# base 10       12,302,652,060,662,200,000
# base 16       0xAABBCCDDEEFF0011
u64 = 0xAABBCCDDEEFF0011L
print 'u64 =', u64, '(0x%x)' % u64
# big endian    0xAA 0xBB 0xCC 0xDD 0xEE 0xFF 0x00 0x11
u64be = struct.pack('>Q', u64)
bx = [byte for byte in struct.unpack('8B', u64be)]
print 'big endian packed', ['0x%02x' % x for x in bx]
assert bx == [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11]
# little endian 0x11 0x00 0xFF 0xEE 0xDD 0xCC 0xBB 0xAA
u64le = struct.pack('<Q', u64)
lx = [byte for byte in struct.unpack('8B', u64le)]
print 'little endian packed', ['0x%02x' % x for x in lx]
assert lx == [0x11, 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa]

check out the documentation for more info: http://docs.python.org/library/struct.html#format-strings

一笔一画续写前缘 2024-12-12 12:50:31

看来您的下一步将是使用struct。类似这样的:

struct.unpack("<f", decode[0:4])

此示例会将 decode 的前四个字节转换为浮点数。查看结构文档以获取有关格式字符串等的更多信息。

Looks like your next step will be to use struct. Something like this:

struct.unpack("<f", decode[0:4])

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文