Python bitstring.BitStream() Examples
The following are 30
code examples of bitstring.BitStream().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
bitstring
, or try the search function
.
Example #1
Source File: test_bitstream.py From bitstring with MIT License | 6 votes |
def testReplaceBitpos(self): a = BitStream('0xff') a.bitpos = 8 a.replace('0xff', '', bytealigned=True) self.assertEqual(a.bitpos, 0) a = BitStream('0b0011110001') a.bitpos = 4 a.replace('0b1', '0b000') self.assertEqual(a.bitpos, 8) a = BitStream('0b1') a.bitpos = 1 a.replace('0b1', '0b11111', bytealigned=True) self.assertEqual(a.bitpos, 5) a.replace('0b11', '0b0', False) self.assertEqual(a.bitpos, 3) a.append('0b00') a.replace('0b00', '0xffff') self.assertEqual(a.bitpos, 17)
Example #2
Source File: test_bitstream.py From bitstring with MIT License | 6 votes |
def testInsertingUsingSetItem(self): a = BitStream() a[0:0] = '0xdeadbeef' self.assertEqual(a, '0xdeadbeef') self.assertEqual(a.bytepos, 4) a[16:16] = '0xfeed' self.assertEqual(a, '0xdeadfeedbeef') self.assertEqual(a.bytepos, 4) a[0:0] = '0xa' self.assertEqual(a, '0xadeadfeedbeef') self.assertEqual(a.bitpos, 4) a.bytepos = 6 a[0:0] = '0xff' self.assertEqual(a.bytepos, 1) a[8:0] = '0x00000' self.assertTrue(a.startswith('0xff00000adead'))
Example #3
Source File: test_bitstream.py From bitstring with MIT License | 6 votes |
def testPackWithLiterals(self): s = bitstring.pack('0xf') self.assertEqual(s, '0xf') self.assertTrue(type(s), BitStream) s = pack('0b1') self.assertEqual(s, '0b1') s = pack('0o7') self.assertEqual(s, '0o7') s = pack('int:10=-1') self.assertEqual(s, '0b1111111111') s = pack('uint:10=1') self.assertEqual(s, '0b0000000001') s = pack('ue=12') self.assertEqual(s.ue, 12) s = pack('se=-12') self.assertEqual(s.se, -12) s = pack('bin=01') self.assertEqual(s.bin, '01') s = pack('hex=01') self.assertEqual(s.hex, '01') s = pack('oct=01') self.assertEqual(s.oct, '01')
Example #4
Source File: test_bitstream.py From bitstring with MIT License | 6 votes |
def testPackWithLengthRestriction(self): s = pack('bin:3', '0b000') self.assertRaises(bitstring.CreationError, pack, 'bin:3', '0b0011') self.assertRaises(bitstring.CreationError, pack, 'bin:3', '0b11') self.assertRaises(bitstring.CreationError, pack, 'bin:3=0b0011') self.assertRaises(bitstring.CreationError, pack, 'bin:3=0b11') s = pack('hex:4', '0xf') self.assertRaises(bitstring.CreationError, pack, 'hex:4', '0b111') self.assertRaises(bitstring.CreationError, pack, 'hex:4', '0b11111') self.assertRaises(bitstring.CreationError, pack, 'hex:8=0xf') s = pack('oct:6', '0o77') self.assertRaises(bitstring.CreationError, pack, 'oct:6', '0o1') self.assertRaises(bitstring.CreationError, pack, 'oct:6', '0o111') self.assertRaises(bitstring.CreationError, pack, 'oct:3', '0b1') self.assertRaises(bitstring.CreationError, pack, 'oct:3=hello', hello='0o12') s = pack('bits:3', BitStream('0b111')) self.assertRaises(bitstring.CreationError, pack, 'bits:3', BitStream('0b11')) self.assertRaises(bitstring.CreationError, pack, 'bits:3', BitStream('0b1111')) self.assertRaises(bitstring.CreationError, pack, 'bits:12=b', b=BitStream('0b11'))
Example #5
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testAppendSameBitstring(self): s1 = BitStream('0xf0')[:6] s1.append(s1) self.assertEqual(s1.bin, '111100111100')
Example #6
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testCreataionFromFileErrors(self): self.assertRaises(IOError, BitStream, filename='Idonotexist')
Example #7
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testFindInFile(self): s = BitStream(filename='test.m1v') self.assertTrue(s.find('0x160120')) self.assertEqual(s.bytepos, 4) s3 = s.read(24) self.assertEqual(s3.hex, '160120') s.bytepos = 0 self.assertTrue(s._pos == 0) self.assertTrue(s.find('0x0001b2')) self.assertEqual(s.bytepos, 13)
Example #8
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testOffset1(self): s = BitStream(bytes=b'\x00\x1b\x3f', offset=4) self.assertEqual(s.read(8).bin, '00000001') self.assertEqual(s.length, 20)
Example #9
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testSeekToBit(self): s = BitStream(bytes=b'\x00\x00\x00\x00\x00\x00') s.bitpos = 0 self.assertEqual(s.bitpos, 0) self.assertRaises(ValueError, s._setbitpos, -1) self.assertRaises(ValueError, s._setbitpos, 6 * 8 + 1) s.bitpos = 6 * 8 self.assertEqual(s.bitpos, 6 * 8)
Example #10
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testSeekToByte(self): s = BitStream(bytes=b'\x00\x00\x00\x00\x00\xab') s.bytepos = 5 self.assertEqual(s.read(8).hex, 'ab')
Example #11
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testAdvanceBitsAndBytes(self): s = BitStream(bytes=b'\x00\x00\x00\x00\x00\x00\x00\x00') s.pos += 5 self.assertEqual(s.pos, 5) s.bitpos += 16 self.assertEqual(s.pos, 2 * 8 + 5) s.pos -= 8 self.assertEqual(s.pos, 8 + 5)
Example #12
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testRetreatBitsAndBytes(self): a = BitStream(length=100) a.pos = 80 a.bytepos -= 5 self.assertEqual(a.bytepos, 5) a.pos -= 5 self.assertEqual(a.pos, 35)
Example #13
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testCreationFromFileOperations(self): s = BitStream(filename='smalltestfile') s.append('0xff') self.assertEqual(s.hex, '0123456789abcdefff') s = ConstBitStream(filename='smalltestfile') t = BitStream('0xff') + s self.assertEqual(t.hex, 'ff0123456789abcdef') s = BitStream(filename='smalltestfile') del s[:1] self.assertEqual((BitStream('0b0') + s).hex, '0123456789abcdef') s = BitStream(filename='smalltestfile') del s[:7 * 8] self.assertEqual(s.hex, 'ef') s = BitStream(filename='smalltestfile') s.insert('0xc', 4) self.assertEqual(s.hex, '0c123456789abcdef') s = BitStream(filename='smalltestfile') s.prepend('0xf') self.assertEqual(s.hex, 'f0123456789abcdef') s = BitStream(filename='smalltestfile') s.overwrite('0xaaa', 12) self.assertEqual(s.hex, '012aaa6789abcdef') s = BitStream(filename='smalltestfile') s.reverse() self.assertEqual(s.hex, 'f7b3d591e6a2c480') s = BitStream(filename='smalltestfile') del s[-60:] self.assertEqual(s.hex, '0') s = BitStream(filename='smalltestfile') del s[:60] self.assertEqual(s.hex, 'f')
Example #14
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testByteAlign(self): s = BitStream(hex='0001ff23') s.bytealign() self.assertEqual(s.bytepos, 0) s.pos += 11 s.bytealign() self.assertEqual(s.bytepos, 2) s.pos -= 10 s.bytealign() self.assertEqual(s.bytepos, 1)
Example #15
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testAppendWithOffset(self): s = BitStream(bytes=b'\x28\x28', offset=1) s.append('0b0') self.assertEqual(s.hex, '5050')
Example #16
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testBitPosition(self): s = BitStream(bytes=b'\x00\x00\x00') self.assertEqual(s.bitpos, 0) s.read(5) self.assertEqual(s.pos, 5) s.pos = s.len self.assertRaises(bitstring.ReadError, s.read, 1)
Example #17
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testAppend(self): s1 = BitStream('0b00000') s1.append(BitStream(bool=True)) self.assertEqual(s1.bin, '000001') self.assertEqual((BitStream('0x0102') + BitStream('0x0304')).hex, '01020304')
Example #18
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testFileSlices(self): s = BitStream(filename='smalltestfile') self.assertEqual(s[-16:].hex, 'cdef')
Example #19
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testCreationFromFileWithOffset(self): a = BitStream(filename='test.m1v', offset=4) self.assertEqual(a.peek(4 * 8).hex, '00001b31') b = BitStream(filename='test.m1v', offset=28) self.assertEqual(b.peek(8).hex, '31')
Example #20
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testHexFromFile(self): s = BitStream(filename='test.m1v') self.assertEqual(s[0:32].hex, '000001b3') self.assertEqual(s[-32:].hex, '000001b7') s.hex = '0x11' self.assertEqual(s.hex, '11')
Example #21
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testUnpack2(self): s = BitStream('0xff, 0b000, uint:12=100') a, b, c = s.unpack('bits:8, bits, uint:12') self.assertEqual(type(s), BitStream) self.assertEqual(a, '0xff') self.assertEqual(type(s), BitStream) self.assertEqual(b, '0b000') self.assertEqual(c, 100) a, b = s.unpack(['bits:11', 'uint']) self.assertEqual(a, '0xff, 0b000') self.assertEqual(b, 100)
Example #22
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testUnpack1(self): s = BitStream('uint:13=23, hex=e, bin=010, int:41=-554, 0o44332, se=-12, ue=4') s.pos = 11 a, b, c, d, e, f, g = s.unpack('uint:13, hex:4, bin:3, int:41, oct:15, se, ue') self.assertEqual(a, 23) self.assertEqual(b, 'e') self.assertEqual(c, '010') self.assertEqual(d, -554) self.assertEqual(e, '44332') self.assertEqual(f, -12) self.assertEqual(g, 4) self.assertEqual(s.pos, 11)
Example #23
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testPackingLongKeywordBitstring(self): s = pack('bits=b', b=BitStream(128000)) self.assertEqual(s, BitStream(128000))
Example #24
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testPackDefualtUintErrors(self): self.assertRaises(bitstring.CreationError, BitStream, '5=-1')
Example #25
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testPackWithDict4(self): s = pack('hello', hello='0xf') self.assertEqual(s, '0xf') s = pack('x, y, x, y, x', x='0b10', y='uint:12=100') t = BitStream('0b10, uint:12=100, 0b10, uint:12=100, 0b10') self.assertEqual(s, t) a = [1, 2, 3, 4, 5] s = pack('int:8, div,' * 5, *a, **{'div': '0b1'}) t = BitStream('int:8=1, 0b1, int:8=2, 0b1, int:8=3, 0b1, int:8=4, 0b1, int:8=5, 0b1') self.assertEqual(s, t)
Example #26
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testPackWithDict2(self): a = pack('int:5, bin:3=b, 0x3, bin=c, se=12', 10, b='0b111', c='0b1') b = BitStream('int:5=10, 0b111, 0x3, 0b1, se=12') self.assertEqual(a, b) a = pack('bits:3=b', b=BitStream('0b101')) self.assertEqual(a, '0b101') a = pack('bits:24=b', b=BitStream('0x001122')) self.assertEqual(a, '0x001122')
Example #27
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testPack1(self): s = bitstring.pack('uint:6, bin, hex, int:6, se, ue, oct', 10, '0b110', 'ff', -1, -6, 6, '54') t = BitStream('uint:6=10, 0b110, 0xff, int:6=-1, se=-6, ue=6, oct=54') self.assertEqual(s, t) with self.assertRaises(bitstring.CreationError): pack('tomato', '0') with self.assertRaises(bitstring.CreationError): pack('uint', 12) with self.assertRaises(bitstring.CreationError): pack('hex', 'penguin') with self.assertRaises(bitstring.CreationError): pack('hex12', '0x12')
Example #28
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testSetSlice(self): a = BitStream() a[0:0] = '0xabcdef' self.assertEqual(a.bytepos, 3) a[4:16] = '' self.assertEqual(a, '0xaef') self.assertEqual(a.bitpos, 4) a[8:] = '0x00' self.assertEqual(a, '0xae00') self.assertEqual(a.bytepos, 2) a += '0xf' a[8:] = '0xe' self.assertEqual(a, '0xaee') self.assertEqual(a.bitpos, 12) b = BitStream() b[0:800] = '0xffee' self.assertEqual(b, '0xffee') b[4:48] = '0xeed123' self.assertEqual(b, '0xfeed123') b[-800:8] = '0x0000' self.assertEqual(b, '0x0000ed123') a = BitStream('0xabcde') self.assertEqual(a[-100:-90], '') self.assertEqual(a[-100:-16], '0xa') a[-100:-16] = '0x0' self.assertEqual(a, '0x0bcde')
Example #29
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testReplaceErrors(self): a = BitStream('0o123415') with self.assertRaises(ValueError): a.replace('', 0o7, bytealigned=True) with self.assertRaises(ValueError): a.replace('0b1', '0b1', start=-100, bytealigned=True) with self.assertRaises(ValueError): a.replace('0b1', '0b1', end=19, bytealigned=True)
Example #30
Source File: test_bitstream.py From bitstring with MIT License | 5 votes |
def testReplaceCount(self): a = BitStream('0x223344223344223344') n = a.replace('0x2', '0x0', count=0, bytealigned=True) self.assertEqual(n, 0) self.assertEqual(a.hex, '223344223344223344') n = a.replace('0x2', '0x0', count=1, bytealigned=True) self.assertEqual(n, 1) self.assertEqual(a.hex, '023344223344223344') n = a.replace('0x33', '', count=2, bytealigned=True) self.assertEqual(n, 2) self.assertEqual(a.hex, '02442244223344') n = a.replace('0x44', '0x4444', count=1435, bytealigned=True) self.assertEqual(n, 3) self.assertEqual(a.hex, '02444422444422334444')