
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Byte String to List in Python
A byte string is similar to a string (Unicode), except instead of characters, it contains a series of bytes. Applications that handle pure ASCII text rather than Unicode text can use byte strings.
Converting a byte string to a list in Python provides compatibility, modifiability, and easier access to individual bytes. It allows seamless integration with other data structures, facilitates modification of elements, and enables efficient analysis and manipulation of specific parts of the byte string.
Demostration
Assume we have taken an input byte string. We will now convert the given byte string to the list of ASCII values as shown below using the above methods.
Input
inputByteString = b'Tutorialspoint'
Output
List of ASCII values of an input byte string: [84, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110, 116]
Methods Used
The following are the various methods to accomplish this task:
Using the list() function
Using the for loop and ord() function
Using the from_bytes() function
Method 1: Using the list() function
This method will demonstrate how to convert a byte string to a list string using the simple list() function of Python.
list() function
Returns a list of an iterable. It means it converts the iterable to a list.
Syntax
list([iterable])
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input byte string.
Use the list() function to convert the input byte string into a list that contains the ASCII values of an input byte by passing the input byte string as an argument and print the resultant list.
Example
The following program converts the input byte string into a list and returns the list of ASCII values of an input byte string using the list() function -
# input byte string inputByteStr = b'Tutorialspoint' # converting the input byte string into a list # which contains the ASCII values as a list of an input byte string print("List of ASCII values of an input byte string:") print(list(inputByteStr))
Output
On executing, the above program will generate the following output
List of ASCII values of an input byte string: [84, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110, 116]
Method 2: Using the for loop and ord() function
In this method we are going to use the combination of simple for loop and ord() function of python to convert a given byte string into a list.
ord() Function
Returns the Unicode code of a given character as a number.
Syntax
ord(char)
Parameters
char: an input byte string.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task
Create a variable to store the input byte string.
Create an empty list for storing the ASCII values of input byte string characters.
Use the for loop to traverse through each character of the input byte string.
Use the ord() function(returns the Unicode code/ASCII value of a given character as a number) to get the ASCII value of each character.
Append it to the resultant list using the append() function(adds the element to the list at the end).
Printing the Unicode/ASCII values of input byte string i.e converting byte string into a list.
Example
The following program converts the input byte string into a list and returns the list of ASCII values of an input byte string using the for loop and ord() function -
# input byte string inputByteStr = 'Tutorialspoint' # empty list for storing the ASCII values of input byte string characters resultantList = [] # traversing through each character of the input byte string for c in inputByteStr: # getting the ASCII value of each character using the ord() function # and appending it to the resultant list resultantList.append(ord(c)) # Printing the Unicode/ASCII values of an input byte string print("List of ASCII values of an input byte string:", resultantList)
Output
On executing, the above program will generate the following output
List of ASCII values of an input byte string: [84, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110, 116]
Method 3: Using the from_bytes() function
In this method we are going to understand how to use from_bytes() function to convert a byte string to a list.
from_bytes() function
To convert the given byte string into the equivalent int values, use the from_bytes() function.
Syntax
int.from_bytes(bytes, byteorder, *, signed=False)
Parameters
bytes: It is a byte object.
byteorder: It is the order of representation of the integer value. In "little" byteorder, the most significant bit is stored at the end and the least significant bit at the beginning, whereas in "big" byteorder, the MSB is placed at the start and the LSB at the end. Big byte order determines an integer's base 256 value.
signed: False is its default value. Whether to represent a number's 2's complement is indicated by this argument.
Example
The following program converts the given input byte string to a list using the from_bytes() function
# input byte string inputByteStr = b'\x00\x01\x002' # Converting the given byte string to int # Here big represents the byte code intValue = int.from_bytes(inputByteStr , "big") # Printing the integer value of an input byte string print(intValue)
Output
65586
Conclusion
This article has taught us three distinct ways to turn a given byte string into a list. Additionally, we learned how to use the ord() function to obtain a character's ASCII value. Finally, we learned how to use the append() function to add elements to the list at the end.