def decode(self, data, parent=None, name=None, little_endian=False): self._verify_not_little_endian(little_endian) container = self._get_struct(name, parent) a = to_tbcd_value(data) index = 0 for field in self._fields.values(): field_length = field.length.decode(container, len(data) * 2 - index) container[field.name] = Field(field.type, field.name, to_tbcd_binary(a[index:index + field_length])) index += field_length return container
def test_to_tbcd_binary(self): self.assertEquals(to_bin("0b11110001"), to_tbcd_binary("1")) self.assertEquals(to_bin("0b00010001"), to_tbcd_binary("11")) self.assertEquals(to_bin("0b11110010"), to_tbcd_binary("2")) self.assertEquals(to_bin("0b00110010"), to_tbcd_binary("23")) self.assertEquals(to_bin("0b0010000111110011"), to_tbcd_binary("123")) self.assertEquals( to_bin("0b0110001000010010000000100000000000000000000000000000000011110001"), to_tbcd_binary("262120000000001"), )
def test_to_tbcd_binary(self): self.assertEqual(to_bin('0b11110001'), to_tbcd_binary('1')) self.assertEqual(to_bin('0b00010001'), to_tbcd_binary('11')) self.assertEqual(to_bin('0b11110010'), to_tbcd_binary('2')) self.assertEqual(to_bin('0b00110010'), to_tbcd_binary('23')) self.assertEqual(to_bin('0b0010000111110011'), to_tbcd_binary('123')) self.assertEqual( to_bin( '0b0110001000010010000000100000000000000000000000000000000011110001' ), to_tbcd_binary('262120000000001'))
def _encode_value(self, value, message, little_endian=False): self._raise_error_if_no_value(value, message) binary = to_tbcd_binary(value) length = self.length.decode(message, len(binary)) return binary, self._byte_length(length)