Make hexprint show proper data length

Historically hexprint zero-pads the data it is printing, which makes
it hard to see how much data is actually there. Fix that.
This commit is contained in:
Dan Smith 2022-11-19 14:55:52 -08:00 committed by Dan Smith
parent 010334ca69
commit 2dcdd1d129
2 changed files with 46 additions and 20 deletions

View File

@ -13,25 +13,32 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import six
import struct
def byte_to_int(b):
"""This does what is needed to convert a bytes()[i] to an int"""
if six.PY3 and isinstance(b, int):
return b
else:
return ord(b)
def hexprint(data, addrfmt=None):
"""Return a hexdump-like encoding of @data"""
if addrfmt is None:
addrfmt = '%(addr)03i'
block_size = 8
lines = len(data) / block_size
if (len(data) % block_size) != 0:
lines += 1
data += "\x00" * ((lines * block_size) - len(data))
out = ""
for block in range(0, (len(data)/block_size)):
blocks = len(data) // block_size
if len(data) % block_size:
blocks += 1
for block in range(0, blocks):
addr = block * block_size
try:
out += addrfmt % locals()
@ -39,22 +46,22 @@ def hexprint(data, addrfmt=None):
out += "%03i" % addr
out += ': '
left = len(data) - (block * block_size)
if left < block_size:
limit = left
else:
limit = block_size
for j in range(0, limit):
out += "%02x " % ord(data[(block * block_size) + j])
for j in range(0, block_size):
try:
out += "%02x " % byte_to_int(data[(block * block_size) + j])
except IndexError:
out += " "
out += " "
for j in range(0, limit):
char = data[(block * block_size) + j]
for j in range(0, block_size):
try:
char = byte_to_int(data[(block * block_size) + j])
except IndexError:
char = ord('.')
if ord(char) > 0x20 and ord(char) < 0x7E:
out += "%s" % char
if char > 0x20 and char < 0x7E:
out += "%s" % chr(char)
else:
out += "."

19
tests/unit/test_util.py Normal file
View File

@ -0,0 +1,19 @@
from chirp import util
from tests.unit import base
class TestUtils(base.BaseTest):
def test_hexprint_with_string(self):
util.hexprint('00000000000000')
def test_hexprint_with_bytes(self):
util.hexprint(b'00000000000000')
def test_hexprint_short(self):
expected = ('000: 00 00 00 00 00 00 00 00 ........\n'
'008: 00 ........\n')
self.assertEqual(expected, util.hexprint(b'\x00' * 9))
def test_hexprint_even(self):
expected = '000: 00 00 00 00 00 00 00 00 ........\n'
self.assertEqual(expected, util.hexprint(b'\x00' * 8))