w3resource

Python: Convert an array to an array of machine values and return the bytes representation


8. Convert an array to machine values and return bytes

Write a Python program to convert an array to an array of machine values and return the bytes representation.

Sample Solution:

Python Code :

from array import *
print("Bytes to String: ")
x = array('b', [119, 51, 114, 101,  115, 111, 117, 114, 99, 101])
s = x.tobytes()
print(s)

Sample Output:

Bytes to String:                                                       
b'w3resource'

For more Practice: Solve these Related Problems:

  • Write a Python program to convert an array of integers to a bytes object using the tobytes() method.
  • Write a Python program to create an array of floats and then display its bytes representation.
  • Write a Python program to compare the bytes representation of arrays with different data types.
  • Write a Python program to implement a function that takes an array and returns its machine values in bytes format.

Go to:


Previous: Write a Python program to append items from inerrable to the end of the array.
Next: Write a Python program to append items from a specified list.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.