Add method to return Bytes or Bits object as hex string #1

Closed
opened 2021-04-13 00:31:46 -05:00 by wp · 1 comment
Owner

for Bits:
return "0\x%02x" % self

for Bytes:
retvalue = "0x"
for Bits in self:
retvalue += "%02x" % Bits

for both classes, add a "hex" method which calls bytes(self).hex(). Add option to include a separator between each byte.

from bits import *
>>> my_bytes = Bytes(bytes.fromhex('deadbeef'))
>>> my_bytes.hex()
'deadbeef'
>>> my_bytes[0].hex()
'de'
>>> 

This can currently be done by casting the Bits/Bytes object to bytes and calling the hex() method. Adding the method to the bits classes allows them to be used similarly to the built-in bytes class. The optional "sep=" can be used to specify a separator charactor.

It appears the built-in bytes.hex() method already includes 'sep' and 'bytes_per_sep' arguments. Therefore, the Bytes and Bits methods can simply be:

return bytes(self).hex(sep=sep, bytes_per_sep=bytes_per_sep)
for Bits: return "0\x%02x" % self for Bytes: retvalue = "0x" for Bits in self: retvalue += "%02x" % Bits for both classes, add a "hex" method which calls bytes(self).hex(). Add option to include a separator between each byte. ```python from bits import * >>> my_bytes = Bytes(bytes.fromhex('deadbeef')) >>> my_bytes.hex() 'deadbeef' >>> my_bytes[0].hex() 'de' >>> ``` This can currently be done by casting the Bits/Bytes object to bytes and calling the hex() method. Adding the method to the bits classes allows them to be used similarly to the built-in bytes class. The optional "sep=" can be used to specify a separator charactor. It appears the built-in bytes.hex() method already includes 'sep' and 'bytes_per_sep' arguments. Therefore, the Bytes and Bits methods can simply be: ```python return bytes(self).hex(sep=sep, bytes_per_sep=bytes_per_sep) ```
Author
Owner

resolved by c63d2dd4bb

resolved by c63d2dd4bb26850e7628cec1ccdf6c71f2dd988f
wp closed this issue 2021-04-13 01:49:12 -05:00
wp added the
enhancement
label 2021-04-13 22:46:16 -05:00
Sign in to join this conversation.
No description provided.