Open In App

Python program to convert a byte string to a list of integers

Last Updated : 15 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python program to illustrate the
# conversion of a byte string
# to a list of integers
 
# Initializing a byte string as GFG
x = b'GFG'
 
# Calling the list() function to
# create a new list of integers that 
# are the ascii values of the byte
# string GFG
print(list(x))


Output:

[71, 70, 71]

Time Complexity: O(n)

Space Complexity: O(n)

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




# Python program to illustrate the
# conversion of a byte string
# to a list of integers
 
# Initializing a byte string
S = "GFG is a CS Portal"
 
nums = []
 
# Calling the for loop to iterate each
# characters of the given byte string
for chr in S:
 
    # Calling the ord() function
    # to convert the specified byte
    # characters to numbers of the unicode
    nums.append(ord(chr))
 
# Printing the unicode of the byte string
print(nums)


Output:

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

Time Complexity: O(n)
Auxiliary Space: O(n)

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




# Python program to illustrate the
# conversion of a byte string
# to a list of integers
 
# Initializing byte value
byte_val = b'\x00\x01'
 
# Converting to int
# byteorder is big where MSB is at start
int_val = int.from_bytes(byte_val, "big")
 
# Printing int equivalent
print(int_val)


Output:

1

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

Python3




# Python program to illustrate the
# conversion of a byte string
# to a list of integers
 
# Initializing a byte string
byte_val = b'\xfc\x00'
 
# 2's complement is enabled in big
# endian byte order format
int_val = int.from_bytes(byte_val, "big", signed="True")
 
# Printing int object
print(int_val)


Output:

-1024

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

Time Complexity: O(n)

Auxiliary Space: O(n)

Method – 4 : using struct module

In this approach, we will use the unpack() method present in the struct module in Python. It is pre-installed so we don’t need to install it externally using pip, just import it and use it.

Syntax of unpack() method –

struct.unpack(‘<‘ or ‘>’ +’B’ or ‘b’ * len(byte_string) , variable_name/byte_string)

Explanation of each parameter –

‘<‘ or ‘>’ : Here we will use ‘<‘ to denote Little-endian and ‘>’ to denote Big-endian.

‘B’ or ‘b’ * len(byte_string) : ‘B’ represents unsigned bytes, whereas ‘b’ represents signed bytes, we need to take care of this ‘bytes’ as if we pass an unsigned byte value in the next parameter and use the signed byte here then it will not give the desired output. In case of len(byte_string) we can either pass a variable which holds the byte string or pass the byte string in-place.

variable_name / byte_string : Here we will either pass the variable in which we have the byte string stored or directly pass the byte string.

Code – 

Python3




import struct
 
# Storing the byte string
bstr = b"GFG is a CS Portal"
 
# using the unpack method of struct
# and passing required values
# here we are Multiplying B with len(bstr)
# and concatenating it with '<' to tell
# that we want to convert a certain length of
# byte string into into list of itnegers using Big-endian
# and the bytes are signed
data_ints = struct.unpack('<' + 'b'*len(bstr), bstr)
 
# Printing the result
print(data_ints)


Output

(71, 70, 71, 32, 105, 115, 32, 97, 32, 67, 83, 32, 80, 111, 114, 116, 97, 108)

Time Complexity – O(n) # n denotes the length of byte string

Auxiliary Space – O(n) # n denotes the length of the unpacked integers list



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads