Python3 Tips and Sample Code
Some helpful code for binary manipulation with Python3
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'))
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)
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)