Python Data Structure: Convert an array to an array of machine values and return the bytes representation
Python Data Structure: Exercise-11 with Solution
Write a Python program to convert an array to an array of machine values and return the bytes representation.
Sample Solution:
Python Code:
import array
import binascii
a = array.array('i', [1,2,3,4,5,6])
print("Original array:")
print('A1:', a)
bytes_array = a.tobytes()
print('Array of bytes:', binascii.hexlify(bytes_array))
Sample Output:
Original array: A1: array('i', [1, 2, 3, 4, 5, 6]) Array of bytes: b'010000000200000003000000040000000500000006000000'
Flowchart:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to convert an array to an ordinary list with the same items.
Next: Write a Python program to read a string and interpreting the string as an array of machine values.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/data-structures-and-algorithms/python-data-structure-exercise-17.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics