Skip to content

Instantly share code, notes, and snippets.

@inmcm
Created August 8, 2016 03:55
Show Gist options
  • Save inmcm/85d605510504c76bdb3d465b1d31bc02 to your computer and use it in GitHub Desktop.
Save inmcm/85d605510504c76bdb3d465b1d31bc02 to your computer and use it in GitHub Desktop.
Byte <-> Strings <-> Ints Conversion Examples
from binascii import hexlify,unhexlify
from struct import unpack, pack
from array import array
# Byte Constants
some_bytes = b'\x12\x34\x56\x78\x9A\xBC\xDE\xF0'
some_string_bytes = b'Hello!'
some_ascii_hex_bytes = b'\x33\x46\x44\x65\x32\x33\x43\x41\x31'
some_other_bytes = b'\x99\xEF\x31\xC4\x25\xD8\x46\x3A'
some_little_packed_bytes = b'\x00\x00\x16\x3e'
some_big_packed_bytes = b'\x3E\x16\x00\x00'
some_base10_bytes = b'\x93\x42\x80'
# Int Constants
some_hex_int = 0x123456
some_basic_int = 2562
some_negative_int = -430
some_ascii_int = 0x3132333470
# String Contants
some_control_string = '\n\a\r\t\f\v\b'
some_string = 'Goodbyte!'
some_hex_string = 'CC123456FF00'
some_int_string = '123456'
# list
some_list_of_hex_chars = ['5', '0', 'A', 'E', '0', '4']
some_list_of_hex_ints = [0x31,0x33,0x56,0x49]
# array
some_u_byte_array = array('B',[64,91,13,2,119,39,204])
some_s_byte_array = array('b', [-110, -25, 20, -92, 76, -12])
some_u_long_array = array('L', [1222333801, 2633920574, 877116574, 7595258, 3588378825])
some_s_short_array = array('h', [-22616, -16310, 16355, -5875, -22715, -14932])
some_char_array = array('B',[0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x0A]) # 'Hello!' string
print('*************')
print('I need an int')
print('*************')
print('Straight Bytes to Int',)
i = int.from_bytes(some_bytes,'big')
print(some_bytes, '->', i, '(', hex(i), ')')
i = int.from_bytes(some_bytes,'little')
print(some_bytes, '->', i, '(', hex(i), ')')
i = int.from_bytes(some_bytes,'little', signed=True)
print(some_bytes, '->', i, '(', hex(i), ')')
print('')
print('Base 10 Bytes to Int',)
i = int(hexlify(some_base10_bytes), 10)
print(some_base10_bytes, '->', i, '(', hex(i), ')')
print('')
print('Single Packed Long Int')
i = unpack('>L',some_little_packed_bytes)[0]
print(some_little_packed_bytes, '->', i, '(', hex(i), ')')
i = unpack('<L',some_big_packed_bytes)[0]
print(some_big_packed_bytes, '->', i, '(', hex(i), ')')
print('Multiple Packed Unsinged Short Ints')
i = unpack('>HHHH',some_bytes)
print(some_bytes, '->', i[0], '(', hex(i[0]), ')', ',', i[1], '(', hex(i[1]), ')', ',', i[2], '(', hex(i[2]), ')', ',', i[3], '(', hex(i[3]), ')')
print('Multiple Packed Short Ints')
i = unpack('<hhhh',some_bytes)
print(some_bytes, '->', i[0], '(', hex(i[0]), ')', ',', i[1], '(', hex(i[1]), ')', ',', i[2], '(', hex(i[2]), ')', ',', i[3], '(', hex(i[3]), ')')
print('Pointer Packed Int')
i = unpack('P', some_other_bytes)
print(some_other_bytes, '->', i[0], '(', hex(i[0]), ')')
print('')
print('*************')
print('I need a string')
print('*************')
p = hexlify(some_other_bytes)
print(p)
r = str(some_bytes)
t = str(some_other_bytes)
print(r)
print(t)
print('')
print('*************')
print('I need bytes')
print('*************')
print('Strings')
b = some_string.encode()
print(some_string, '->', b)
b = unhexlify(some_hex_string)
print(some_hex_string, '->', b)
print('\nIntegers')
b= some_basic_int.to_bytes(2,'little')
print(some_basic_int, '->', b)
b= some_basic_int.to_bytes(4,'big')
print(some_basic_int, '->', b)
b= some_negative_int.to_bytes(3,'big',signed=True)
print(some_negative_int, '->', b)
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment