我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用binascii.crc_hqx()。
def test_returned_value(self): # Limit to the minimum of all limits (b2a_uu) MAX_ALL = 45 raw = self.rawdata[:MAX_ALL] for fa, fb in zip(a2b_functions, b2a_functions): a2b = getattr(binascii, fa) b2a = getattr(binascii, fb) try: a = b2a(self.type2test(raw)) res = a2b(self.type2test(a)) except Exception as err: self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) if fb == 'b2a_hqx': # b2a_hqx returns a tuple res, _ = res self.assertEqual(res, raw, "{}/{} conversion: " "{!r} != {!r}".format(fb, fa, res, raw)) self.assertIsInstance(res, bytes) self.assertIsInstance(a, bytes) self.assertLess(max(a), 128) self.assertIsInstance(binascii.crc_hqx(raw, 0), int) self.assertIsInstance(binascii.crc32(raw), int)
def test_returned_value(self): # Limit to the minimum of all limits (b2a_uu) MAX_ALL = 45 raw = self.rawdata[:MAX_ALL] for fa, fb in zip(a2b_functions, b2a_functions): a2b = getattr(binascii, fa) b2a = getattr(binascii, fb) try: a = b2a(self.type2test(raw)) res = a2b(self.type2test(a)) except Exception, err: self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) if fb == 'b2a_hqx': # b2a_hqx returns a tuple res, _ = res self.assertEqual(res, raw, "{}/{} conversion: " "{!r} != {!r}".format(fb, fa, res, raw)) self.assertIsInstance(res, str) self.assertIsInstance(a, str) self.assertLess(max(ord(c) for c in a), 128) self.assertIsInstance(binascii.crc_hqx(raw, 0), int) self.assertIsInstance(binascii.crc32(raw), int)
def __new__( self, SYNC=b'\xaa\x01', IDCODE=1, TIME=None, TQ_FLAGS='0000', MSG_TQ='1111', TIME_BASE=16777215, DATA=b'' ): TIME = time.time() if not TIME else TIME raw_data = b''.join(( SYNC, (len(DATA) + 16).to_bytes(2, 'big'), # FRAMESIZE IDCODE.to_bytes(2, 'big'), int(TIME).to_bytes(4, 'big'), # SOC int(TQ_FLAGS + MSG_TQ, 2).to_bytes(1, 'big'), int(TIME % 1 * TIME_BASE).to_bytes(3, 'big'), # Frac of sec DATA )) chk_sum = binascii.crc_hqx(raw_data, -1).to_bytes(2, 'big') return bytes.__new__(bytes, raw_data+chk_sum)
def _write(self, data): self.crc = binascii.crc_hqx(data, self.crc) self.ofp.write(data)
def _writecrc(self): # XXXX Should this be here?? # self.crc = binascii.crc_hqx('\0\0', self.crc) if self.crc < 0: fmt = '>h' else: fmt = '>H' self.ofp.write(struct.pack(fmt, self.crc)) self.crc = 0
def _read(self, len): data = self.ifp.read(len) self.crc = binascii.crc_hqx(data, self.crc) return data
def _checkcrc(self): filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff #self.crc = binascii.crc_hqx('\0\0', self.crc) # XXXX Is this needed?? self.crc = self.crc & 0xffff if filecrc != self.crc: raise Error, 'CRC error, computed %x, read %x' \ %(self.crc, filecrc) self.crc = 0
def crc(cls, framing, data): '''CRC-16 with an initial value that depends on the packet format''' return binascii.crc_hqx(data, cls.formats[framing]['initial_crc_value'])
def __init__(self, filename): self.data = open(filename, 'rb').read() stored_crc16, size0, size1, size2 = struct.unpack('<HHHH', self.data[:8]) calc_crc16 = binascii.crc_hqx(self.data[2:], 0xffff) if calc_crc16 != stored_crc16: raise ValueError("Unexpected CRC, found %04X but calculated %04X" % (stored_crc16, calc_crc16)) self.sizes = (size0, size1, size2)
def test_empty_string(self): # A test for SF bug #1022953. Make sure SystemError is not raised. empty = self.type2test(b'') for func in all_functions: if func == 'crc_hqx': # crc_hqx needs 2 arguments binascii.crc_hqx(empty, 0) continue f = getattr(binascii, func) try: f(empty) except Exception as err: self.fail("{}({!r}) raises {!r}".format(func, empty, err))
def test_unicode_strings(self): # Unicode strings are not accepted. for func in all_functions: try: self.assertRaises(TypeError, getattr(binascii, func), "test") except Exception as err: self.fail('{}("test") raises {!r}'.format(func, err)) # crc_hqx needs 2 arguments self.assertRaises(TypeError, binascii.crc_hqx, "test", 0)
def _checkcrc(self): filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff #self.crc = binascii.crc_hqx('\0\0', self.crc) # XXXX Is this needed?? self.crc = self.crc & 0xffff if filecrc != self.crc: raise Error('CRC error, computed %x, read %x' % (self.crc, filecrc)) self.crc = 0
def test_crc_hqx(self): crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0) crc = binascii.crc_hqx(self.type2test(b" this string."), crc) self.assertEqual(crc, 14290) self.assertRaises(TypeError, binascii.crc_hqx) self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b''))
def test_empty_string(self): # A test for SF bug #1022953. Make sure SystemError is not raised. empty = self.type2test('') for func in all_functions: if func == 'crc_hqx': # crc_hqx needs 2 arguments binascii.crc_hqx(empty, 0) continue f = getattr(binascii, func) try: f(empty) except Exception, err: self.fail("{}({!r}) raises {!r}".format(func, empty, err))
def test_unicode_b2a(self): # Unicode strings are not accepted by b2a_* functions. for func in set(all_functions) - set(a2b_functions) | {'rledecode_hqx'}: try: self.assertRaises(TypeError, getattr(binascii, func), "test") except Exception as err: self.fail('{}("test") raises {!r}'.format(func, err)) # crc_hqx needs 2 arguments self.assertRaises(TypeError, binascii.crc_hqx, "test", 0)
def _frame(self, control, data): """Construct a frame""" crc = binascii.crc_hqx(control + data, 0xffff) crc = bytes([crc >> 8, crc % 256]) return self._stuff(control + data + crc) + self.FLAG
def send(self, b, end=False): """Send up to 7 bytes of data. :param bytes b: 0 - 7 bytes of data to transmit. :param bool end: If this is the last data. """ assert len(b) <= 7, "Max 7 bytes can be sent" if not end: assert len(b) == 7, "Less than 7 bytes only allowed if last data" self._seqno += 1 command = self._seqno if end: command |= NO_MORE_BLOCKS self._done = True # Change expected ACK:ed sequence self._blksize = self._seqno # Save how many bytes this message contains since this is the last self._last_bytes_sent = len(b) request = bytearray(8) request[0] = command request[1:len(b) + 1] = b self.sdo_client.send_request(request) self.pos += len(b) if self.crc_supported: # Calculate CRC self._crc = binascii.crc_hqx(b, self._crc) if self._seqno >= self._blksize: # End of this block, wait for ACK self._block_ack()
def test_crc_hqx(self): crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0) crc = binascii.crc_hqx(self.type2test(b" this string."), crc) self.assertEqual(crc, 14290) self.assertRaises(TypeError, binascii.crc_hqx) self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b'')) for crc in 0, 1, 0x1234, 0x12345, 0x12345678, -1: self.assertEqual(binascii.crc_hqx(self.type2test(b''), crc), crc & 0xffff)