def write_bytes(self, value, nbytes=None):
import bitstring
# TODO: strings are utf-8 from json reading
if isinstance(value, six.text_type):
value = value.encode('latin-1')
value_len = len(value)
# Ensure the string is under the required data width
if nbytes is None:
nbytes = value_len
else:
if value_len > nbytes:
value = value[:nbytes]
elif value_len < nbytes:
value += b' ' * (nbytes - value_len)
# Cannot use string format shortcut, i.e. 'bytes:{}={}' due to the
# automatic whitespace trimming by bitstring.
self.bit_stream += bitstring.Bits(bytes=value)
return value
评论列表
文章目录