Tôi có thể chuyển đổi một danh sách các int thành một mảng byte với các mục sau:
bytes[[17, 24, 121, 1, 12, 222, 34, 76]]
Out[144]: b'\x11\x18y\x01\x0c\xde"L'
bytes[[1, 2, 3]]
Out[145]: b'\x01\x02\x03'
Điều tôi muốn bây giờ là đưa chuỗi mảng byte trở lại danh sách ban đầu của nó. Có một chức năng Python dễ dàng để làm điều đó? Tôi đã tìm thấy như sau:. Is there an easy python function to do that? I found the following:
int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
Tôi không hoàn toàn chắc chắn những gì đang xảy ra ở đây. Làm thế nào là chuyển đổi xảy ra và những gì đầu ra biểu thị. Vì vậy, nếu bất cứ ai có thể cung cấp một số bối cảnh hoặc cái nhìn sâu sắc, tôi sẽ biết ơn
Đưa ra một chuỗi byte. Nhiệm vụ là viết một chương trình Python để chuyển đổi byte chuỗi này thành danh sách các số nguyên. & NBSP;
Phương pháp 1: Bằng cách sử dụng hàm list []By using list[] function
Hàm Danh sách [] được sử dụng để tạo một danh sách từ số khác được chỉ định được thực hiện dưới dạng tham số của nó. list[] function is used to create a list from the specified iterable taken as its parameter.
Syntax:
list[[iterable]]
Tham số: Hàm này chấp nhận một tham số duy nhất được minh họa bên dưới:This function accepts a single parameter that is illustrated below:
- Có thể lặp lại: Đây là chuỗi được chỉ định sẽ được tạo như một danh sách khác.This is the specified sequence that is going to be created as another list.
Giá trị trả về: Hàm này trả về một danh sách mới được tạo ra từ số đó đã được thông qua dưới dạng đối số của nó. This function returns a new list created out of the given iterable passed as its arguments.
Ví dụ: Chương trình Python cho chuỗi byte vào danh sách các số nguyênPython program to a byte string to a list of integers
Python3
Output:
[71, 70, 71]
[71, 70, 71]
3=
[71, 70, 71]
5[71, 70, 71]
6By using
for loop and ord[] function
[71, 70, 71]7
=
[71, 70, 71]9
10
11
12ord[] function is used to return the number representing the Unicode code of a specified byte character.
Syntax:
ord[character]
Ví dụ 2: Chương trình Python cho chuỗi byte vào danh sách các số nguyênThis function accepts a single parameter which is illustrated below:
[71, 70, 71]3=
[71, 70, 71]518 This is the specified byte string.
[71, 70, 71]7=
[71, 70, 71]91011-10244=
-1024612This function returns the number representing the Unicode code of a specified byte character.
Ví dụ: Chương trình Python cho chuỗi byte vào danh sách các số nguyênPython program to a byte string to a list of integers
Python3
S
=
"GFG is a CS Portal"
nums
=
int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
0int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
1 int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
2 int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
3 int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
4int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
5int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
6int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
7int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
8int.from_bytes[b'\x11\x18y\x01\x0c\xde"L', byteorder='big', signed=False]
Out[146]: 1231867543503643212
2[71, 70, 71]0
[71, 70, 71]1
[71, 70, 71]2
Output:
[71, 70, 71, 32, 105, 115, 32, 97, 32, 67, 83, 32, 80, 111, 114, 116, 97, 108]
Phương pháp 3: Bằng cách sử dụng hàm from_bytes [] By using from_bytes[] function
Hàm from_Bytes [] được sử dụng để chuyển đổi chuỗi byte được chỉ định thành các giá trị int tương ứng của nó.from_bytes[] function is used to convert the specified byte string into its corresponding int values.
Syntax:
int.from_bytes [byte, byteorder, *, đã ký = false]
Tham số: Hàm này chấp nhận một số tham số được minh họa dưới đây: This function accepts some parameters which are illustrated below:
- Byte: Đối tượng byteA byte object
- ByteOrder: Tham số này xác định thứ tự biểu diễn của giá trị số nguyên. Byteorder có thể có các giá trị như là một trong những điều đáng kể nhất được lưu trữ ở cuối và ít nhất là ở đầu, hoặc lớn, nơi MSB được lưu trữ khi bắt đầu và LSB ở cuối. Thứ tự byte lớn tính toán giá trị của một số nguyên trong cơ sở 256.This parameter determines the order of representation of the integer value. byteorder can have values as either “little” where most significant bit is stored at the end and least at the beginning, or big, where MSB is stored at start and LSB at the end. Big byte order calculates the value of an integer in base 256.
- Đã ký: Giá trị mặc định của nó là sai. Tham số này cho biết có thể đại diện cho 2 bổ sung của một số hay không. Its default value is False. This parameter Indicates whether to represent 2’s complement of a number.
Giá trị trả về: Hàm này trả về một int tương đương với byte đã cho. This function returns an int equivalent to the given byte.
Ví dụ: Chương trình Python cho chuỗi byte vào danh sách các số nguyênPython program to a byte string to a list of integers
Python3
[71, 70, 71]3
=
[71, 70, 71]5
[71, 70, 71]6
[71, 70, 71]7
=
[71, 70, 71]9
10
11
12
[71, 70, 71]1
14
Output:
1
Ví dụ 2: Chương trình Python cho chuỗi byte vào danh sách các số nguyênPython program to a byte string to a list of integers
Python3
[71, 70, 71]3
=
[71, 70, 71]5
18
[71, 70, 71]7
=
[71, 70, 71]9
10
11
-10244
=
-10246
12
[71, 70, 71]1
14
Output:
-1024
Độ phức tạp về thời gian và không gian của tất cả các phương pháp là như nhau ::
Độ phức tạp về thời gian: O [n]O[n]
Không gian phụ trợ: O [n]O[n]