要将数字列表解码为字节数组,可以使用 Python 的内置函数 bytes()bytearray()

使用 bytes() 函数:

num_list = [65, 66, 67, 68]
byte_arr = bytes(num_list)
print(byte_arr)  # b'ABCD'

使用 bytearray() 函数:

num_list = [65, 66, 67, 68]
byte_arr = bytearray(num_list)
print(byte_arr)  # bytearray(b'ABCD')

在以上代码中,我们先定义了一个数字列表 num_list,然后将其传递给 bytes()bytearray() 函数,生成一个字节数组对象 byte_arr。注意 bytes() 函数返回的是不可变的字节对象,而 bytearray() 函数返回的是可变的字节数组对象。最后我们打印出字节数组对象的值,得到了解码后的结果。

Python 字节数组解码:将数字列表转换为字节数组

原文地址: https://www.cveoy.top/t/topic/mSQ3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录