繁体   English   中英

在python中将8位列表转换为32位整数数组

[英]Convert a 8bit list to a 32 bit integer array in python

我有的 :

textdata = "this is my test data"
DataArray = [ord(c) for c in textdata]

现在我想通过将列表的4个元素组合在一起将其转换为x 32位整数

例如:DataArray [0:4]将成为32位整数,然后迭代到接下来的4个元素并执行相同的操作。 最后,我将得到一个包含所有结果的32位数组。

我如何在python遍历整个字符串的过程中做到这一点。 有没有简单的方法可以做到这一点?

只要您的字符串是4的整数倍,就可以以非常有效的方式使用NumPy:

import numpy as np
data = np.fromstring(textdata, dtype='>u4')
# array([1952999795,  543781664, 1836654708, 1702065184, 1684108385])

'>u4'表示“大端无符号4字节整数”。

编辑 :如果您使用NumPy> = 1.14,则不np.fromstring使用np.fromstring ,处理文本的正确方法是调用np.frombuffer(textdata.encode(), dtype='>u4')

使用numpy:

>>> import numpy as np

>>> a = np.frombuffer(b'this is my test data', dtype=np.int32)
>>> a
array([1936287860,  544434464, 1948285293,  544502629, 1635017060], dtype=int32)
>>> a.tobytes()
b'this is my test data'

使用'<i4'或类似的dtype表示在机器之间可移植的字节序。

我假设您可以将初始数据保留为bytes而不是unicode ,因为您确实应该努力做到这一点。

您可以使用struct内置的python模块:

from struct import unpack

textdata = "this is my test data"
data = list(unpack('i'*(len(textdata)//4), textdata))

结果:

[1936287860, 544434464, 1948285293, 544502629, 1635017060]

例如,如果要使用无符号整数,则不需要遍历字符串,并且可以找到其他格式字符

您可以使用类似以下的东西,它使用位操作(大端):

def chunk2int(chunk):
    """ Converts a chunk (string) into an int, 8 bits per character """
    val = 0
    for c in chunk:
        val = (val << 8) | (ord(c) & 0xFF)
    return val

def int2chunk(val):
    """ Converts an int into a chunk, consuming 8 bits per character """
    rchunk = []
    while val:
        rchunk.append(val & 0xFF)
        val >>= 8

    return ''.join(chr(c) for c in reversed(rchunk))

textdata = "this is my test data"

chunks = [textdata[i:i + 4] for i in range(0, len(textdata), 4)]
print(chunks)

data = [chunk2int(c) for c in chunks]
print(data)

chunks = [int2chunk(d) for d in data]
print(chunks)

生产:

['this', ' is ', 'my t', 'est ', 'data']
[1952999795, 543781664, 1836654708, 1702065184, 1684108385]
['this', ' is ', 'my t', 'est ', 'data']

如果您在输入文本中使用1 <= ord(c) <= 255的字符,则可以使用。 如果您的字符串中int2chunk字节,则int2chunk方法可能会提前终止,在这种情况下,您必须填充这些块。

还有struct模块,可能值得研究,在这里您可以更简单地更改字节序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM