from bitstring import Bits, pack bits = pack('uint:5, uint:1, uint:10, uint:8', 2, 0, 64, 2) byte1, byte2, byte3 = bits.unpack('bytes:1,bytes:1,bytes:1') listOfBytes = [byte1, byte2, byte3] checksum = sum(map(ord, listOfBytes)) if checksum > 511: checksum = checksum - 512 if checksum > 255: checksum = checksum - 256 if checksum > 127: checksum = checksum - 128 bits = pack('uint:5, uint:1, uint:10, uint:8, uint:8', 2, 0, 64, 2, checksum)