numpy.ndarray.tobytes#

method

ndarray.tobytes(order='C')#

Construct Python bytes containing the raw data bytes in the array.

Constructs Python bytes showing a copy of the raw contents of data memory. The bytes object is produced in C-order by default. This behavior is controlled by the order parameter.

Parameters:
order{β€˜C’, β€˜F’, β€˜A’}, optional

Controls the memory layout of the bytes object. β€˜C’ means C-order, β€˜F’ means F-order, β€˜A’ (short for Any) means β€˜F’ if a is Fortran contiguous, β€˜C’ otherwise. Default is β€˜C’.

Returns:
sbytes

Python bytes exhibiting a copy of a’s raw data.

See also

frombuffer

Inverse of this operation, construct a 1-dimensional array from Python bytes.

Examples

>>> import numpy as np
>>> x = np.array([[0, 1], [2, 3]], dtype='<u2')
>>> x.tobytes()
b'\x00\x00\x01\x00\x02\x00\x03\x00'
>>> x.tobytes('C') == x.tobytes()
True
>>> x.tobytes('F')
b'\x00\x00\x02\x00\x01\x00\x03\x00'