Python: Read a string and interpreting the string as an array of machine values
22. Read a String and Interpret It as an Array of Machine Values
Write a Python program that reads a string and interprets it as an array of machine values.
Sample Solution:
Python Code:
from array import array
import binascii
array1 = array('i', [7, 8, 9, 10])
print('array1:', array1)
as_bytes = array1.tobytes()
print('Bytes:', binascii.hexlify(as_bytes))
array2 = array('i')
array2.frombytes(as_bytes)
print('array2:', array2)
Sample Output:
array1: array('i', [7, 8, 9, 10]) Bytes: b'0700000008000000090000000a000000' array2: array('i', [7, 8, 9, 10])
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to convert a given string into an array of its ASCII values using the array module.
- Write a Python program to interpret a string as bytes, store it in an array, and then print the array.
- Write a Python program to create an array from a string's byte representation and then convert it back to a string.
- Write a Python program to implement a function that takes a string, converts it to an array of machine values, and returns its bytes representation.
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to get the array size of types unsigned integer and float.
Next: Write a Python program to remove all duplicate elements from a given array and returns a new array.
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