简体   繁体   English

在python中将bytearray转换为short int

[英]Converting bytearray to short int in python

I have a long bytearray 我有一个很长的bytearray

barray=b'\x00\xfe\x4b\x00...

What would be the best way to convert it to a list of 2-byte integers? 将它转换为2字节整数列表的最佳方法是什么?

You can use the struct package for that: 您可以使用struct包:

from struct import unpack

tuple_of_shorts = unpack('h'*(len(barray)//2),barray)

This will produce signed shorts. 这将产生签名的短裤。 For unsigned ones, use 'H' instead: 对于未签名的,请使用'H'代替:

tuple_of_shorts = unpack('H'*(len(barray)//2),barray)

This produces on a little-endian machine for your sample input: 这会在little-endian机器上生成样本输入:

>>> struct.unpack('h'*(len(barray)//2),barray)
(-512, 75)
>>> struct.unpack('H'*(len(barray)//2),barray)
(65024, 75)

In case you want to work with big endian , or little endian, you can put a > (big endian) or < (little endian) in the specifications. 如果您想使用big endian或little endian,可以在规范中添加> (big endian)或< (little endian)。 For instance: 例如:

# Big endian
tuple_of_shorts = unpack('>'+'H'*(len(barray)//2),barray)  # unsigned
tuple_of_shorts = unpack('>'+'h'*(len(barray)//2),barray)  # signed

# Little endian
tuple_of_shorts = unpack('<'+'H'*(len(barray)//2),barray)  # unsigned
tuple_of_shorts = unpack('<'+'h'*(len(barray)//2),barray)  # signed

Generating: 正在生成:

>>> unpack('>'+'H'*(len(barray)//2),barray)  # big endian, unsigned
(254, 19200)
>>> unpack('>'+'h'*(len(barray)//2),barray)  # big endian, signed
(254, 19200)
>>> unpack('<'+'H'*(len(barray)//2),barray)  # little endian, unsigned
(65024, 75)
>>> unpack('<'+'h'*(len(barray)//2),barray)  # little endian, signed
(-512, 75)

Using the struct module: 使用struct模块:

import struct

count = len(barray)/2
integers = struct.unpack('H'*count, barray)

Depending on the endianness you may want to prepend a < or > for the unpacking format. 根据字节顺序,您可能希望为解包格式添加<> And depending on signed/unsigned, it's h , or H . 根据签名/未签名,它是hH

If memory efficiency is a concern, you may consider using an array.array : 如果考虑内存效率,可以考虑使用array.array

>>> barr = b'\x00\xfe\x4b\x00'
>>> import array
>>> short_array = array.array('h', barr)
>>> short_array
array('h', [-512, 75])

This is like a space-efficient primitive array, with an OO-wrapper, so it supports sequence-type methods you would have on a list , like .append , .pop , and slicing! 这就好比一个节省空间的基本数组,用面向对象的封装器,所以它支持序列类型的方法你将有一个对list ,像.append.pop ,和切片!

>>> short_array[:1]
array('h', [-512])
>>> short_array[::-1]
array('h', [75, -512])

Also, recovering your bytes object becomes trivial: 此外,恢复您的bytes对象变得微不足道:

>>> short_array
array('h', [-512, 75])
>>> short_array.tobytes()
b'\x00\xfeK\x00'

Note, if you want the opposite endianness from the native byte-order, use the in-place byteswap method: 注意,如果您希望从本机字节顺序获得相反的字节序,请使用就地byteswap方法:

>>> short_array.byteswap()
>>> short_array
array('h', [254, 19200])

Note, using the Python struct library to convert your array also allows you to specify a repeat count for each item in the format specifier. 注意,使用Python struct库转换数组还允许您为格式说明符中的每个项指定重复计数。 So 4H for example would be the same as using HHHH . 因此, 4H例如与使用HHHH相同。

Using this approach avoids the need to create potentially massive format strings: 使用此方法可避免创建可能大规模格式字符串的需要:

import struct

barray = b'\x00\xfe\x4b\x00\x4b\x00'
integers = struct.unpack('{}H'.format(len(barray)/2), barray)

print(integers)

Giving you: 给你:

(65024, 75, 75)

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

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