xct's notes
Search…
Python

Bitstring manipulation

1
def bitstring_to_bytes(s):
2
v = int(s, 2)
3
b = bytearray()
4
while v:
5
b.append(v & 0xff)
6
v >>= 8
7
return bytes(b[::-1])
8
9
base = "010101..." # the bit string
10
n = 8 # change something every n bits
11
result = ""
12
13
# Iterate over bitstring, doing something every n bits
14
for i in xrange(n,len(base)-n,n):
15
# do manipulation here
16
result += num
17
print(bitstring_to_bytes(result))
Copied!

Named Capture Groups

1
m = re.search('[e]\s:\s(?P<e>\d+){0,1}',text.decode('utf-8'))
2
if m != None and 'e' in m.groupdict():
3
e = int(m.group('e'))
Copied!
Last modified 9mo ago