Pages

Friday, August 29, 2014

Python: read and write binary files








def copy(if_path, of_path):
    with open(if_path, 'rb') as in_file, open(of_path, 'wb') as out_file:
        while True:
            chunk = in_file.read(1024)
            if chunk:
                out_file.write(chunk)
                out_file.flush()
            else:
                break



Python: mutable byte array as buffer








bytearray function can be used to make a mutable byte array as buffer:
import struct
buffer = bytearray(8)
buffer[0]=1
buffer[1]=1
struct.pack_into('>ih', buffer, 2, 100, 200)
# to convert the array to str
print(bytes(buffer))

see also




Thursday, August 28, 2014

python: join lists








list1 = ['a', 'b']
list2 = ['c', 'd']

list3 = list1 + list2



Python: encode integers to byte string








import struct
def int_to_bytes(value, size, big_endian=False):
    bs = []
    for i in range(size):
        offset = i * 8
        b = chr((value & (0xff << offset)) >> offset)
        if big_endian:
            bs.insert(0, b)
        else:
            bs.append(b)
    return b''.join(bs)

def int32_to_bytes(value, big_endian=False):
    return int_to_bytes(value, 4, big_endian)

def int64_to_bytes(value, big_endian=False):
    return int_to_bytes(value, 8, big_endian)

if __name__ == '__main__':
    # the two commands below do the same
    print(repr(int64_to_bytes(65536, big_endian=True)))
    print(repr(struct.pack('>Q', 65536)))

see also




python: print a string as hex bytes









':'.join(x.encode('hex') for x in 'Hello World!')



Executes in python CLI:
>>> ':'.join(x.encode('hex') for x in 'Hello World!')
'48:65:6c:6c:6f:20:57:6f:72:6c:64:21'



join binary strings in Python










b''.join(['abc', '\x01\x02\x03', 'efg'])




Execute it in python cli:
>>> b''.join(['abc', '\x01\x02\x03', 'efg'])
'abc\x01\x02\x03efg'



Python: get the length of a utf-8 encoded string








len(s.encode('utf-8'))