Bytes to list int python

I can convert a list of ints into a byte array with the following:

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'

What I want now is to get the byte array string back to its original list. 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

I am not quite sure what is happening here. How is the conversion happening and what does the output signify. So if anyone can provide some context or insight, I will be grateful

Given a byte string. The task is to write a Python program to convert this byte of string to a list of integers. 

Method 1: By using list() function

The list() function is used to create a list from the specified iterable taken as its parameter.

Syntax:

list([iterable])

Parameters: This function accepts a single parameter that is illustrated below:

  • iterable: This is the specified sequence that is going to be created as another list.

Return values: This function returns a new list created out of the given iterable passed as its arguments.

Example: Python program to a byte string to a list of integers

Python3

Output:

[71, 70, 71]

Method 2: By using for loop and ord() function

The ord() function is used to return the number representing the Unicode code of a specified byte character.

Syntax:

ord(character)

Parameters: This function accepts a single parameter which is illustrated below:

  • character: This is the specified byte string.

Return values: This function returns the number representing the Unicode code of a specified byte character.

Example: Python program to a byte string to a list of integers

Python3

S = "GFG is a CS Portal"

nums = []

for chr in S:

    nums.append(ord(chr))

print(nums)

Output:

[71, 70, 71, 32, 105, 115, 32, 97, 32, 67, 83, 32, 80, 111, 114, 116, 97, 108]

Method 3: By using from_bytes() function

The from_bytes() function is used to convert the specified byte string into its corresponding int values.

Syntax:

int.from_bytes(bytes, byteorder, *, signed=False)

Parameters: This function accepts some parameters which are illustrated below:

  • bytes: A byte object
  • byteorder: 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.
  • signed: Its default value is False. This parameter Indicates whether to represent 2’s complement of a number.

Return values: This function returns an int equivalent to the given byte.

Example: Python program to a byte string to a list of integers

Python3

byte_val = b'\x00\x01'

int_val = int.from_bytes(byte_val, "big")

print(int_val)

Output:

1

Example 2: Python program to a byte string to a list of integers

Python3

byte_val = b'\xfc\x00'

int_val = int.from_bytes(byte_val, "big", signed="True")

print(int_val)

Output:

-1024

The time and space complexity of all the methods is same::

Time Complexity: O(n)

Auxiliary Space: O(n)


How do you convert bytes to integers in Python?

To convert bytes to int in Python, use the int. from_bytes() method. A byte value can be interchanged to an int value using the int. from_bytes() function.

Can we assign byte to int?

We can directly assign the byte to the int data type. Secondly, we have a Wrapper class method intValue() that returns the value of byte as an int after widening the primitive conversion as we're storing a smaller data type into a larger one. If we take the byte as unsigned, then we have the Byte.

What does bytes () in Python do?

The bytes() function returns a bytes object. It can convert objects into bytes objects, or create empty bytes object of the specified size.

How do you create a byte array in Python?

bytearray() takes three optional parameters: source (Optional) - source to initialize the array of bytes. encoding (Optional) - if the source is a string, the encoding of the string. errors (Optional) - if the source is a string, the action to take when the encoding conversion fails (Read more: String encoding)