Implement hexdumping

Not used yet, though
master
LEdoian 10 months ago
parent 497dc20949
commit 28f170fe81

@ -8,6 +8,7 @@ from stat import * # suggested usage…
import selectors
from dataclasses import dataclass
import logging as log
import math
# Note: get/setrlimit are in resource module
def setup_logging():
@ -21,9 +22,19 @@ class Direction:
sigil: str
color: str
def hexdump(data: bytes) -> str:
# TODO: dump somehow
...
def hexdump(data: bytes) -> list[str]: # We will be prefixing the lines with sigils, so no need to join.
result = []
offset = 0
offset_len = math.ceil(math.log(len(data), 16))
while data:
left_chunk, right_chunk, data = data[:8], data[8:16], data[16:]
left_ascii = ''.join(chr(x) if x in range(32,127) else ',' for x in left_chunk)
right_ascii = ''.join(chr(x) if x in range(32,127) else ',' for x in right_chunk)
offset_str = f'{offset:0{offset_len}x}'
line = f"{offset_str} {left_chunk.hex(' ').ljust(23)} {right_chunk.hex(' ').ljust(23)} |{left_ascii.ljust(8)}{right_ascii.ljust(8)}|"
offset += 16
result.append(line)
return result
def fdinfo(fd: int) -> str:
stat = os.fstat(fd)

Loading…
Cancel
Save