Overview

Python2.7 is like second nature to me now and I keep getting hung up on the idiotic typing that Python3 has introduced. Hopefully these examples will help.

Binary Data and Hex Ecoding

import binascii

string_example = "test"
byte_array_example = b"test"

# Convert string into bytes
print(string_example.encode('utf-8'))

# Convert byte array into string
print(byte_array_example.decode('utf-8'))

# Convert string into hex encoded byte array
print(binascii.hexlify(string_example.encode('utf-8')))

# Convert byte array into hex encoded byte array
print(binascii.hexlify(byte_array_example))

# Convert byte array into hex encoded string
print(binascii.hexlify(byte_array_example).decode('utf-8'))

# Convert hex encoded byte array into ascii byte array
hex_byte_array = b'74657374'
print(binascii.unhexlify(hex_byte_array))

# Convert hex encoded string into ascii byte array
hex_string = '74657374'
print(binascii.unhexlify(hex_string.encode('utf-8')))

# Convert hex encoded string into ascii string
hex_string = '74657374'
print(binascii.unhexlify(hex_string.encode('utf-8')).decode('utf-8'))
b'test'
test
b'74657374'
b'74657374'
74657374
b'test'
b'test'
test

Hex Encoding Helper Functions

def unhex(hex_string):
    import binascii
    if type(hex_string) == str:
        return binascii.unhexlify(hex_string.encode('utf-8'))
    else:
        return binascii.unhexlify(hex_string)

def tohex(data):
    import binascii
    if type(data) == str:
        return binascii.hexlify(data.encode('utf-8'))
    else:
        return binascii.hexlify(data)

Strings Functions

def unicode_strings(buf, n=4):
    import re
    ASCII_BYTE = b' !\"#\$%&\'\(\)\*\+,-\./0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\]\^_`abcdefghijklmnopqrstuvwxyz\{\|\}\\\~\t'
    if type(buf) == str:
        buf = buf.encode('utf-8')
    reg = b'((?:[%s]\x00){%d,})' % (ASCII_BYTE, n)
    uni_re = re.compile(reg)
    out = []
    for match in uni_re.finditer(buf):
        try:
            out.append(match.group().decode("utf-16"))
        except UnicodeDecodeError:
            pass
    return out


def ascii_strings(buf, n=4):
    import re
    ASCII_BYTE = b' !\"#\$%&\'\(\)\*\+,-\./0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\]\^_`abcdefghijklmnopqrstuvwxyz\{\|\}\\\~\t'
    if type(buf) == str:
        buf = buf.encode('utf-8')
    reg = b'([%s]{%d,})' % (ASCII_BYTE, n)
    ascii_re = re.compile(reg)
    out = []
    for match in ascii_re.finditer(buf):
        try:
            out.append(match.group().decode("ascii"))
        except UnicodeDecodeError:
            pass
    return out
test_var = "test"
print(test_var)
test