将4字节整数打包成bytearray或数组 [英] Pack 4 byte integers into bytearray or array

查看:178
本文介绍了将4字节整数打包成bytearray或数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到如何做到这一点,所以它毕竟可能不那么简单。我看了这篇文章,< a href =https://stackoverflow.com/questions/5302374/packing-4-integers-as-one-byte>这一个,以及其他许多人,但我无法得到答案的类型我在找。我读过的大多数帖子都使用 struct.pack struct.pack_into ,但问题是我不知道我真的知道我需要的数组大小的修道院,我不想创建一个临时存储整数值。所以我认为我可以使用 bytearray ,但我不知道如何添加4字节整数。

I can't seem to find out how to do this, so it may not be that simple after all. I looked this post, this one, and many others, but I can't get the type of answer I'm looking for. Most of the posts I read use the struct.pack or struct.pack_into, but the problem is that I don't really know a priory the size of the array I need, and I wouldn't like to create one to store temporarily the integer values. So I thought that I could use the bytearray instead, but I don't know how to add 4-byte integers to it.

>>> b = bytearray()
>>> for i in range(100):
...   b.append(i)
... 
>>> print(b)
bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abc')
>>> print(list(b))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> import sys
>>> sys.getsizeof(b)
161

因此字节数组不包含4字节整数(这就是我需要的。)那么我将不得不使用某种带零的填充来获得4字节的整数?是否有一个函数可以用来转换传递给整数类型的整数我想要?

So the byte array doesn't contain 4-byte integers (which is what I need). So then I would have to use some sort of padding with zeros in order to get 4-byte integers? Is there a function that I can use to convert the integer passed to append to the type of integer I want?

推荐答案

使用 数组输入以存储固定大小的二进制数据,其中值的数量是可变的:

Use the array type to store fixed-sized binary data, where the number of values is variable:

import array

values = array.array('I')
values.fromlist([i for i in range(100)])
bytes = values.tobytes()

什么是C类型你挑选取决于你的系统架构;您可能必须根据 array.itemsize 属性选择一个;在我的64位Mac上使用4个字节来存储无符号整数:

What C type you pick depends on your system architecture; you may have to pick one based on the array.itemsize attribute; on my 64-bit Mac I uses 4 bytes to store an unsigned integer:

>>> import array
>>> values = array.array('I')
>>> values.itemsize
4
>>> values.fromlist([i for i in range(100)])
>>> len(values.tobytes())
400

字节顺序也取决于机器;如果你需要big-endian字节,但你的机器使用little-endian架构,请使用 array.byteswap() 在转换为字节之前交换订单:

The byte order is also machine dependent; if you need to have big-endian bytes, but your machine uses a little-endian architecture, use array.byteswap() to swap the order before converting to bytes:

import sys

if sys.byteorder == 'little':
    values.byteswap()  # convert to big-endian before writing to a file
values.tofile(fileobj)

这篇关于将4字节整数打包成bytearray或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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