本文整理汇总了Python中bitstring.BitArray类的典型用法代码示例。如果您正苦于以下问题:Python BitArray类的具体用法?Python BitArray怎么用?Python BitArray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BitArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ttl_read
def ttl_read(self):
sp = self.create_WASYNC_PAR()
sp.s_Type = L_ASYNC_TTL_INP
self.io_async(sp)
ret = BitArray(uint=sp.Data[0], length=16)
ret.reverse()
return ret
示例2: main
def main():
"""
1. Get file.
2. Convert from base64 to hex string representation.
3. Convert hex string representation to BitArray.
4. Feed into Repeating_XOR_Breaker
"""
cipher_source = "https://cryptopals.com/static/challenge-data/6.txt"
cipher_reader = Data_reader(cipher_source)
data = cipher_reader.get_data()
data = ''.join(data)
converted = Base64_To_Hex(data).convert()
data_to_break = BitArray(hex=converted)
breaker = Repeating_XOR_Breaker(data_to_break)
candidate_keys = breaker.solve()
print "Candidate Keys:", candidate_keys
for key_set in candidate_keys:
whole_key = BitArray(hex='0x00')
for key in key_set:
whole_key.append(BitArray(int=key, length=8))
print "whole_key:", whole_key
decrypter = ExtendedKeyDecrypter()
decrypter.set_decrypt_key(whole_key.hex)
print decrypter.decrypt()
示例3: get_bits
def get_bits(length, mode=-1):
"""生成指定长度的位串.
length -- 位串长度
mode -- 0 返回全0
1 返回全1
-1 返回随机位串
"""
# 生成指定长度的位串的最大值
bits = BitArray(length)
bits.set(1)
bin_str = ''
if mode == 0:
bits.set(0)
bin_str = '0b' + bits.bin
elif mode == 1:
bin_str = '0b' + bits.bin
else:
# print 'all_1_bit:bin:' + bits.bin
# print 'all_1_bit:uint:' + bits.uint.__str__()
# 生成随机数,0到最大值
random_num = random.randint(0, bits.uint)
# print 'random_num:' + random_num.__str__()
bin_str = bin(random_num)
# print 'created bit:' + bin_str[2:]
return bin_str
示例4: pointer_to_binary
def pointer_to_binary (self, pointer, configuration, thread = None):
"""Convert a Pointer object to the binary init load value for a Programmed Offset entry."""
# Add the Default Offset for each thread, so we point to the correct per-thread instance of the pointed-to variable
# None thread means a shared variable holds the init data, or other unique location identical to all threads.
if thread is not None:
default_offset = configuration.default_offset.offsets[thread]
else:
default_offset = 0
# Addresses wrap around when the offset is added,
# so express negative values as the modular sum value
# Read and write pointers have different address ranges and offset bit widths
if "D" in pointer.memory:
# write pointer
offset = (pointer.base + pointer.offset - pointer.address + default_offset) % configuration.memory_depth_words_write
offset = BitArray(uint=offset, length=configuration.po_write_offset_bit_width)
else:
# read pointer
offset = (pointer.base + pointer.offset - pointer.address + default_offset) % configuration.memory_depth_words
offset = BitArray(uint=offset, length=configuration.po_read_offset_bit_width)
# The increment is a signed magnitude number (absolute value and sign bit)
increment = BitArray(uint=abs(pointer.incr), length=configuration.po_increment_bits)
increment_sign = self.to_sign_bit(pointer.incr)
increment_sign = BitArray(uint=increment_sign, length=configuration.po_increment_sign_bits)
# Now pack them into the Programmed Offset entry binary configurations
pointer_bits = BitArray()
for entry in [increment_sign, increment, offset]:
pointer_bits.append(entry)
return pointer_bits
示例5: p_expression_concat
def p_expression_concat(p):
"expression : expression CONCAT expression"
s1=BitArray(p[1])
s2=BitArray(p[3])
# print str(s2)
s1.append('0b' + s2.bin)
p[0] = '0b' + s1.bin
示例6: team
def team(self):
c = BitArray()
c.append("uint:8=%s" % self.player_slot)
if c[0] == 0:
return "radiant"
else:
return "dire"
示例7: prime_sieve
def prime_sieve(top=10005):
b = BitArray(top) # bitstring of ’0’ bits
for i in range(2, top):
if not b[i]:
yield i
# i is prime, so set all its multiples to ’1’.
b.set(True, range(i * i, top, i))
示例8: initFile
def initFile(self):
file_name = self.file_to_stream['name']
fout = open(file_name, "wb")
data = BitArray(int(self.file_to_stream_length)*8)
data = data.tobytes()
fout.write(data)
fout.close()
示例9: testCopyMethod
def testCopyMethod(self):
s = BitArray(9)
t = s.copy()
self.assertEqual(s, t)
t[0] = True
self.assertEqual(t.bin, '100000000')
self.assertEqual(s.bin, '000000000')
示例10: booths
def booths(m,r):
x=len(bin(m))
y=len(bin(r))
totallength = x+y+1
if m<0 and r<0 or r<0:
bugbit = 1
else:
bugbit = 0
A = BitArray(int = m,length = totallength) << (y+1)
compliment = BitArray(int = -m,length = totallength) << (y+1)
P = BitArray(int = r, length = totallength)
P = P<<1
for i in range(1,y+1):
if P[-2:]=='0b01':
P = BitArray(int = P.int + A.int, length = totallength)
elif P[-2:]=='0b10':
P = BitArray(int = P.int + compliment.int, length = totallength)
P = BitArray(int = P.int>>1, length = totallength)
P = P[:-1]
P.int = P.int + bugbit
steps =""
return '<h1>RESULT</h1><br>'+steps+'<br><h3>decimal value: '+str(P.int)+'</br><br> binary value: '+str(P.bin)+"</h3>"
示例11: MasterKey
class MasterKey(object):
def __init__(self, master_key_hex_string="0x3cc849279ba298b587a34cabaeffc5ecb3a044bbf97c516fab7ede9d1af77cfa"):
self.key = BitArray(master_key_hex_string)
self.session_keys_amount = 8
self.current_cycle_index = 0
self.master_key_round_shift_bits = 24
def get_round_keys(self):
"""
:return: list of round keys
:rtype: list[RoundKey]
"""
if self.current_cycle_index:
round_master_key = self.key.copy()
round_master_key.ror(self.master_key_round_shift_bits * self.current_cycle_index)
else:
round_master_key = self.key.copy()
round_keys = []
round_key_size = round_master_key.length / self.session_keys_amount
for key_index in range(0, self.session_keys_amount):
round_key = round_master_key[key_index * round_key_size:key_index * round_key_size + round_key_size]
round_keys.append(RoundKey(round_key))
if self.current_cycle_index < 8:
self.current_cycle_index += 1
else:
self.current_cycle_index = 0
return round_keys
示例12: generate_coded_symbol
def generate_coded_symbol(generator_row, message_symbol_list):
selected_list = [message_symbol_list[i] for i, g in enumerate(generator_row) if g == 1]
coded_symbol = BitArray( [0] * len(message_symbol_list[0]) )
for s in selected_list:
coded_symbol = coded_symbol.__xor__(s)
return coded_symbol
示例13: huff_encode
def huff_encode(text):
freq = defaultdict(int)
for s in text:
freq[s] += 1
tree = [ [f, [s, ""]] for s,f in freq.items() ]
heapify(tree)
while len(tree) > 1:
l = heappop(tree)
h = heappop(tree)
for n in l[1:]:
n[1] = '0' + n[1]
for n in h[1:]:
n[1] = '1' + n[1]
heappush(tree, [l[0] + h[0]] + l[1:] + h[1:])
root = heappop(tree)[1:]
codes = dict([(s, "0b"+c) for s,c in root])
# Header
enc = BitArray()
for s,c in root:
enc += BitArray(bytes=s)
enc += BitArray(uint=len(c), length=8)
enc += BitArray("0b"+c)
enc.prepend(BitArray(uint=len(root), length=8))
for s in text:
enc += BitArray(codes[s])
return enc
示例14: render
def render(self, ctx=None):
'''
:param ctx: rendering context in which the method was called
:rtype: `Bits`
:return: rendered value of the container
'''
self._initialize()
if ctx is None:
ctx = RenderContext()
ctx.push(self)
self.set_offset(self._offset, ctx)
if self.is_default():
self._current_rendered = self._default_rendered
ctx.pop()
return self._default_rendered
rendered = BitArray()
offset = 0
for field in self._fields:
frendered = field.render(ctx)
if not isinstance(frendered, Bits):
raise KittyException('the field %s:%s was rendered to type %s, you should probably wrap it with appropriate encoder' % (
field.get_name(), type(field), type(frendered)))
rendered.append(frendered)
offset += len(frendered)
self.set_current_value(rendered)
ctx.pop()
return self._current_rendered
示例15: carryDigits
def carryDigits(digits):
bits = BitArray(bin='000')
for digit in digits:
bits = bits ^ digit
bits.rol(1)
return bits