Python Data Structure: Convert an array to an array of machine values and return the bytes representation
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics