Write the content of 'buf' out in a hexdump style

    Args:
        fout: file object to write to
        buf: the buffer to be pretty printed
        offset: the starting offset of the buffer
        width: how many bytes should be displayed per row

def write_hex(fout, buf, offset, width=16):
    """Write the content of 'buf' out in a hexdump style

    Args:
        fout: file object to write to
        buf: the buffer to be pretty printed
        offset: the starting offset of the buffer
        width: how many bytes should be displayed per row
    """

    skipped_zeroes = 0
    for i, chunk in enumerate(chunk_iter(buf, width)):
        # zero skipping
        if chunk == (b"\x00" * width):
            skipped_zeroes += 1
            continue
        elif skipped_zeroes != 0:
            fout.write("  -- skipped zeroes: {}\n".format(skipped_zeroes))
            skipped_zeroes = 0

        # starting address of the current line
        fout.write("{:016x}  ".format(i * width + offset))

        # bytes column
        column = "  ".join([" ".join(["{:02x}".format(c) for c in subchunk])
                            for subchunk in chunk_iter(chunk, 8)])
        w = width * 2 + (width - 1) + ((width // 8) - 1)
        if len(column) != w:
            column += " " * (w - len(column))
        fout.write(column)

        # ASCII character column
        fout.write("  |")
        for c in chunk:
            if c in PRINTABLE_CHARS:
                fout.write(chr(c))
            else:
                fout.write(".")
        if len(chunk) < width:
            fout.write(" " * (width - len(chunk)))
        fout.write("|")

        fout.write("\n")