int.to_bytes()
int.to_bytes() is a Python method in the Number Methods category. Returns an array of bytes representing the integer. The syntax is int.to_bytes(length, byteorder, *, signed=False). Parameters: length, byteorder — byte configuration. Returns: Bytes object. A typical example: (42).to_bytes(2, "big") # b"\x00*"
(42).to_bytes(2, "little") # b"*\x00". A close sibling is int.bit_length(), which returns the number of bits required to represent the integer in binary. A close sibling is int.bit_count(), which returns the number of ones in the binary representation of the integer. A close sibling is int.from_bytes(), which returns the integer represented by the given array of bytes. A close sibling is float.is_integer(), which returns True if the float is finite with integral value. More about this category: Numeric type methods — int, float, complex operations and conversions. Related Number Methods entries: int.bit_length(), int.bit_count(), int.from_bytes(), float.is_integer(), float.as_integer_ratio(), float.hex(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.