From a985b4593d3f0c788c5e6ef0066bf82ae550cfb8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 9 Apr 2014 00:40:58 +0300 Subject: objint: Implement int.from_bytes() class method and .to_bytes() method. These two are apprerently the most concise and efficient way to convert int to/from bytes in Python. The alternatives are struct and array modules, but methods using them are more verbose in Python code and less efficient in memory/cycles. --- tests/basics/int-bytes.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/basics/int-bytes.py (limited to 'tests/basics/int-bytes.py') diff --git a/tests/basics/int-bytes.py b/tests/basics/int-bytes.py new file mode 100644 index 000000000..45965ed46 --- /dev/null +++ b/tests/basics/int-bytes.py @@ -0,0 +1,6 @@ +print((10).to_bytes(1, "little")) +print((111111).to_bytes(4, "little")) +print((100).to_bytes(10, "little")) +print(int.from_bytes(b"\x00\x01\0\0\0\0\0\0", "little")) +print(int.from_bytes(b"\x01\0\0\0\0\0\0\0", "little")) +print(int.from_bytes(b"\x00\x01\0\0\0\0\0\0", "little")) -- cgit v1.2.3