From d6d7114eec5c827dbd20af6b45234119662ba076 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Tue, 24 Aug 2021 15:33:50 -0400 Subject: [PATCH] Committing initial intro work --- .gitignore | 1 + bluetooth/README.md | 7 +++++ python_binary/README.md | 7 +++++ python_binary/binary_utils.py | 8 +++++ python_binary/intro_bytes.py | 39 +++++++++++++++++++++++ python_binary/struct_intro.py | 59 +++++++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 bluetooth/README.md create mode 100644 python_binary/README.md create mode 100644 python_binary/binary_utils.py create mode 100644 python_binary/intro_bytes.py create mode 100644 python_binary/struct_intro.py diff --git a/.gitignore b/.gitignore index 21637a0..b58de0c 100644 --- a/.gitignore +++ b/.gitignore @@ -182,6 +182,7 @@ cython_debug/ *.su *.idb *.pdb +.idea # Kernel Module Compile Results *.mod* diff --git a/bluetooth/README.md b/bluetooth/README.md new file mode 100644 index 0000000..cd43143 --- /dev/null +++ b/bluetooth/README.md @@ -0,0 +1,7 @@ +# Bluetooth + +This section contains code or examples in interacting with Bluetooth from the Mac. + +## Library + +This code is primarily built using [Pybluez](https://github.com/pybluez/pybluez). \ No newline at end of file diff --git a/python_binary/README.md b/python_binary/README.md new file mode 100644 index 0000000..746fa33 --- /dev/null +++ b/python_binary/README.md @@ -0,0 +1,7 @@ +# Learning binary with Python + +This section is targeted towards the interaction with binary data from Python. + +## Resources + +- https://pymotw.com/2/struct/ \ No newline at end of file diff --git a/python_binary/binary_utils.py b/python_binary/binary_utils.py new file mode 100644 index 0000000..dd14b2d --- /dev/null +++ b/python_binary/binary_utils.py @@ -0,0 +1,8 @@ +""" +Contains various helper functions for dealing with binary or hex values in Python. +""" + + +def get_bin(value, padn=0, sep=None): + """Prints a binary textual representation of a value""" + return format(value, 'b').zfill(padn) \ No newline at end of file diff --git a/python_binary/intro_bytes.py b/python_binary/intro_bytes.py new file mode 100644 index 0000000..74e35d7 --- /dev/null +++ b/python_binary/intro_bytes.py @@ -0,0 +1,39 @@ +""" +Module explores the builtins functions for working with some binary data. +""" +import sys + +print("This device's native byteorder is: {}\n".format(sys.byteorder)) + +# One word (16 bit) Hex number +dead_hex = 0xDEAD # 57005 +beef_hex = 0xBEEF +print("hex: {} int: {} bin: {} bitlength: {}\n".format(hex(dead_hex), dead_hex, bin(dead_hex), dead_hex.bit_length())) + +# Binary numbers + +deadbeef_bin = 0xDEADBEEF + +print("int.bin_to_bytes type : {}".format(type(deadbeef_bin.to_bytes(4, byteorder="big")))) # immutable type +print("0xDEADBEEF big endian bin : {}".format(deadbeef_bin.to_bytes(4, byteorder="big"))) +print("0xDEADBEEF little endian bin : {}".format(deadbeef_bin.to_bytes(4, byteorder="little"))) + +print("0xDEADBEEF in native endian : {}".format(deadbeef_bin.to_bytes(4, byteorder=sys.byteorder))) + +# Error checking in to bytes +try: + deadbeef_bin.to_bytes(1, byteorder=sys.byteorder) +except OverflowError: + print("Caught an overflow error when too few bytes were attempted to represent a 32bit value") + +print("Sign matters. Signed binary values use 2's complement.") +value = 0b11111111 # 255 unsigned -1 when signed +bytes_value = value.to_bytes(1, byteorder=sys.byteorder) +print("Unsigned value: {}".format(int.from_bytes(bytes_value, byteorder=sys.byteorder, signed=False))) +print("signed value: {}".format(int.from_bytes(bytes_value, byteorder=sys.byteorder, signed=True))) + + +## Binascii + +# As it says in the standard lib docs this package is for converting binary/ascii + diff --git a/python_binary/struct_intro.py b/python_binary/struct_intro.py new file mode 100644 index 0000000..65b6746 --- /dev/null +++ b/python_binary/struct_intro.py @@ -0,0 +1,59 @@ +import struct +import binascii + + +def get_bin(value, padn=0, sep=None): + """Prints a binary textual representation of a value in """ + return format(value, 'b').zfill(padn) + + +# Packing a data structure + +values = (1, b'ab', 2.7) +s = struct.Struct('I 2s f') +packed_data = s.pack(*values) + +print("\n##### Struct Packing Example #####\n\n") +print('Original values :', values) +print('Packed Type :', type(packed_data)) +# This is most likely going to contain non ascii characters when printed. +# So it's not suitable to copy it over the printed string and then try to +# hexlify +print('Packed print :', packed_data) +print('Format string :', s.format) +print('Uses :', s.size, 'bytes') +print('Packed Value (hex) :', binascii.hexlify(packed_data)) +# binascii.hexlify(data[, sep[, bytes_per_sep=1]]) can also make more +# human readable values +print('Spaced Value (hex) :', binascii.hexlify(packed_data, b"_", 4)) + +# Unpacking a data structure + +packed_data = binascii.unhexlify('0100000061620000cdcc2c40') + +unpacked_data = s.unpack(packed_data) + +print("\n##### Struct Unpacking Example #####\n\n") +print('Unpacked Values:', unpacked_data) + +# Endianness in binary data + +print("\n##### Endianness Demonstration #####\n\n") +print('Original values:', values) + +endianness = [ + ('@', 'native, native'), + ('=', 'native, standard'), + ('<', 'little-endian'), + ('>', 'big-endian'), + ('!', 'network'), +] + +for code, name in endianness: + s = struct.Struct(code + ' I 2s f') + packed_data = s.pack(*values) + + print('Format string :', s.format, 'for', name) + print('Uses :', s.size, 'bytes') + print('Packed Value :', binascii.hexlify(packed_data)) + print('Unpacked Value :', s.unpack(packed_data), "\n")