Committing initial intro work

master
Drew Bednar 3 years ago
parent ab6a12f875
commit d6d7114eec

1
.gitignore vendored

@ -182,6 +182,7 @@ cython_debug/
*.su
*.idb
*.pdb
.idea
# Kernel Module Compile Results
*.mod*

@ -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).

@ -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/

@ -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)

@ -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

@ -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")
Loading…
Cancel
Save